﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class Main_ServiceInvoice_SalesInvoice : System.Web.UI.Page
{
    Datautility DU = new Datautility();

    #region Declare list ControlID

    int counter = 2;

    List<string> LstRowCounter;

    List<string> LstTdServiceCtrl;
    List<string> LstDdlService;

    List<string> LstHidTaxableNonTaxable;
    List<string> LsthidServiceIncomeLid;

    List<string> LstTdDescriptionCtrl;
    List<string> LstTxtDescriptionCtrl;

    List<string> LstTdYearRefCtrl;
    List<string> LstTxtYearRefCtrl;

    List<string> LstTdUomCtrl;
    List<string> LstTxtUomCtrl;

    List<string> LstTdUnit2Ctrl;
    List<string> LstTxtUnit2Ctrl;
    List<string> LstTdUnit3Ctrl;
    List<string> LstTxtUnit3Ctrl;
    List<string> LstTdUnit4Ctrl;
    List<string> LstTxtUnit4Ctrl;

    List<string> LstTdUnitRateCtrl;
    List<string> LstTxtUnitRateCtrl;
    List<string> LstTdRateCtrl;
    List<string> LstTxtRateCtrl;

    List<string> LstTdGrossAmtCtrl;
    List<string> LstTxtGrossAmtCtrl;

    List<string> LstTdDiscountRateCtrl;
    List<string> LstTxtDiscountRateCtrl;
    List<string> LstTdDiscountAmtCtrl;
    List<string> LstTxtDiscountAmtCtrl;

    List<string> LstTdServiceChargeCtrl;
    List<string> LstTxtServiceChargeCtrl;
    List<string> listhidServiceCharge;
    List<string> LstTdServiceChargeAmtCtrl;
    List<string> LstTxtServiceChargeAmtCtrl;
    List<string> LstTdServiceChargeTotalCtrl;
    List<string> LstTxtServiceChargeTotalCtrl;

    List<string> LstTdServiceTaxCtrl;
    List<string> LstTxtServiceTaxCtrl;
    List<string> LstTdPECessCtrl;
    List<string> LstTxtPECessCtrl;
    List<string> LstTdHECessCtrl;
    List<string> LstTxtHECessCtrl;
    List<string> LstTdLuxuryCtrl;
    List<string> LstTxtLuxuryCtrl;
    List<string> LstTdVATCtrl;
    List<string> LstTxtVATCtrl;

    List<string> LstTdTaxIncGrandTotalCtrl;
    List<string> LstTxtTaxIncGrandTotalCtrl;

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        OverrideAddMore();
    }

    #endregion End Declare ControlID

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region Initilize ControlID

            LstRowCounter = new List<string>();
            ViewState["LstRowCounter"] = LstRowCounter;

            LstTdServiceCtrl = new List<string>();
            ViewState["LstTdServiceCtrl"] = LstTdServiceCtrl;
            LstDdlService = new List<string>();
            ViewState["LstDdlService"] = LstDdlService;

            LstHidTaxableNonTaxable = new List<string>();
            ViewState["LstHidTaxableNonTaxable"] = LstHidTaxableNonTaxable;

            LsthidServiceIncomeLid = new List<string>();
            ViewState["LsthidServiceIncomeLid"] = LsthidServiceIncomeLid;


            LstTdDescriptionCtrl = new List<string>();
            ViewState["LstTdDescriptionCtrl"] = LstTdDescriptionCtrl;
            LstTxtDescriptionCtrl = new List<string>();
            ViewState["LstTxtDescriptionCtrl"] = LstTxtDescriptionCtrl;

            LstTdYearRefCtrl = new List<string>();
            ViewState["LstTdYearRefCtrl"] = LstTdYearRefCtrl;
            LstTxtYearRefCtrl = new List<string>();
            ViewState["LstTxtYearRefCtrl"] = LstTxtYearRefCtrl;

            LstTdUomCtrl = new List<string>();
            ViewState["LstTdUomCtrl"] = LstTdUomCtrl;
            LstTxtUomCtrl = new List<string>();
            ViewState["LstTxtUomCtrl"] = LstTxtUomCtrl;

            LstTdUnit2Ctrl = new List<string>();
            ViewState["LstTdUnit2Ctrl"] = LstTdUnit2Ctrl;
            LstTxtUnit2Ctrl = new List<string>();
            ViewState["LstTxtUnit2Ctrl"] = LstTxtUnit2Ctrl;
            LstTdUnit3Ctrl = new List<string>();
            ViewState["LstTdUnit3Ctrl"] = LstTdUnit3Ctrl;
            LstTxtUnit3Ctrl = new List<string>();
            ViewState["LstTxtUnit3Ctrl"] = LstTxtUnit3Ctrl;
            LstTdUnit4Ctrl = new List<string>();
            ViewState["LstTdUnit4Ctrl"] = LstTdUnit4Ctrl;
            LstTxtUnit4Ctrl = new List<string>();
            ViewState["LstTxtUnit4Ctrl"] = LstTxtUnit4Ctrl;

            LstTdUnitRateCtrl = new List<string>();
            ViewState["LstTdUnitRateCtrl"] = LstTdUnitRateCtrl;
            LstTxtUnitRateCtrl = new List<string>();
            ViewState["LstTxtUnitRateCtrl"] = LstTxtUnitRateCtrl;
            LstTdRateCtrl = new List<string>();
            ViewState["LstTdRateCtrl"] = LstTdRateCtrl;
            LstTxtRateCtrl = new List<string>();
            ViewState["LstTxtRateCtrl"] = LstTxtRateCtrl;

            LstTdGrossAmtCtrl = new List<string>();
            ViewState["LstTdGrossAmtCtrl"] = LstTdGrossAmtCtrl;
            LstTxtGrossAmtCtrl = new List<string>();
            ViewState["LstTxtGrossAmtCtrl"] = LstTxtGrossAmtCtrl;

            LstTdDiscountRateCtrl = new List<string>();
            ViewState["LstTdDiscountRateCtrl"] = LstTdDiscountRateCtrl;
            LstTxtDiscountRateCtrl = new List<string>();
            ViewState["LstTxtDiscountRateCtrl"] = LstTxtDiscountRateCtrl;
            LstTdDiscountAmtCtrl = new List<string>();
            ViewState["LstTdDiscountAmtCtrl"] = LstTdDiscountAmtCtrl;
            LstTxtDiscountAmtCtrl = new List<string>();
            ViewState["LstTxtDiscountAmtCtrl"] = LstTxtDiscountAmtCtrl;

            LstTdServiceChargeCtrl = new List<string>();
            ViewState["LstTdServiceChargeCtrl"] = LstTdServiceChargeCtrl;
            LstTxtServiceChargeCtrl = new List<string>();
            ViewState["LstTxtServiceChargeCtrl"] = LstTxtServiceChargeCtrl;
            listhidServiceCharge = new List<string>();
            ViewState["listhidServiceCharge"] = listhidServiceCharge;
            LstTdServiceChargeAmtCtrl = new List<string>();
            ViewState["LstTdServiceChargeAmtCtrl"] = LstTdServiceChargeAmtCtrl;
            LstTxtServiceChargeAmtCtrl = new List<string>();
            ViewState["LstTxtServiceChargeAmtCtrl"] = LstTxtServiceChargeAmtCtrl;
            LstTdServiceChargeTotalCtrl = new List<string>();
            ViewState["LstTdServiceChargeTotalCtrl"] = LstTdServiceChargeTotalCtrl;
            LstTxtServiceChargeTotalCtrl = new List<string>();
            ViewState["LstTxtServiceChargeTotalCtrl"] = LstTxtServiceChargeTotalCtrl;

            LstTdServiceTaxCtrl = new List<string>();
            ViewState["LstTdServiceTaxCtrl"] = LstTdServiceTaxCtrl;
            LstTxtServiceTaxCtrl = new List<string>();
            ViewState["LstTxtServiceTaxCtrl"] = LstTxtServiceTaxCtrl;
            LstTdPECessCtrl = new List<string>();
            ViewState["LstTdPECessCtrl"] = LstTdPECessCtrl;
            LstTxtPECessCtrl = new List<string>();
            ViewState["LstTxtPECessCtrl"] = LstTxtPECessCtrl;
            LstTdHECessCtrl = new List<string>();
            ViewState["LstTdHECessCtrl"] = LstTdHECessCtrl;
            LstTxtHECessCtrl = new List<string>();
            ViewState["LstTxtHECessCtrl"] = LstTxtHECessCtrl;
            LstTdLuxuryCtrl = new List<string>();
            ViewState["LstTdLuxuryCtrl"] = LstTdLuxuryCtrl;
            LstTxtLuxuryCtrl = new List<string>();
            ViewState["LstTxtLuxuryCtrl"] = LstTxtLuxuryCtrl;
            LstTdVATCtrl = new List<string>();
            ViewState["LstTdVATCtrl"] = LstTdVATCtrl;
            LstTxtVATCtrl = new List<string>();
            ViewState["LstTxtVATCtrl"] = LstTxtVATCtrl;

            LstTdTaxIncGrandTotalCtrl = new List<string>();
            ViewState["LstTdTaxIncGrandTotalCtrl"] = LstTdTaxIncGrandTotalCtrl;
            LstTxtTaxIncGrandTotalCtrl = new List<string>();
            ViewState["LstTxtTaxIncGrandTotalCtrl"] = LstTxtTaxIncGrandTotalCtrl;

            #endregion End Initilize ControlID

            _BindFormat();
            _BindCustomer();
            ddlFormat_SelectedIndex(this, EventArgs.Empty);

            txtDate.Text = string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(System.DateTime.Now.ToString()));

        }
        _GetCurrentServiceTaxRate(txtDate.Text);



    }
    /// <summary>
    /// this function bind transaction format from IVC_TransactionFormat database
    /// table where Transaction type =1 , 1 means Service Invoicing
    /// </summary>
    private void _BindFormat()
    {
        SqlParameter[] sparam = { 
                                 new SqlParameter("@TRANSACTIONTYPE","1")
                               };
        DataTable DTFormat = DU.getdatausingprocedure("USP_IVC_GetAllTransactionFormatNameID", sparam);
        if (DTFormat.Rows.Count > 0)
        {
            ddlFormat.DataSource = DTFormat;
            ddlFormat.DataTextField = "FORMATNAME";
            ddlFormat.DataValueField = "FORMATID";
            ddlFormat.DataBind();
            ddlFormat.Items.Insert(0, new ListItem("--Select Format--", "0"));
        }
    }
    /// <summary>
    /// This function bind all the service from service master
    /// database table
    /// </summary>
    /// <param name="ddl"></param>
    private void _BindService(DropDownList ddl)
    {
        if (ddlFormat.SelectedValue != "0")
        {
            DataTable DTService = DU.getdatausingprocedure("USP_IVC_GetAllServices");
            if (DTService.Rows.Count > 0)
            {
                ddl.DataSource = DTService;
                ddl.DataTextField = "SERVICENAME";
                ddl.DataValueField = "SERVICEID";
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("--Select Service--", "0"));
            }
        }
    }
    /// <summary>
    /// This functions is used for reset all
    /// control in a parent control
    /// </summary>
    /// <param name="parentControl"></param>
    private void _ResetControls(Control parentControl)
    {
        foreach (Control c in parentControl.Controls)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)(c)).Text = string.Empty;
            }
            if (c.GetType() == typeof(HiddenField))
            {
                ((HiddenField)(c)).Value = string.Empty;
            }
            if (c.GetType() == typeof(DropDownList))
            {
                if (((DropDownList)(c)).Items.Count > 0)
                {
                    ((DropDownList)(c)).SelectedValue = "0";
                }
            }
            if (c.HasControls())
            {
                _ResetControls(c);
            }
        }
    }
    /// <summary>
    /// Bind customer/vender to dropdownlist control
    /// the customer or vender should be sundry creditor/debtors
    /// </summary>
    private void _BindCustomer()
    {
        DataTable dtCustomer = DU.getdatausingprocedure("USP_IVC_GetCustomerLedger");
        if (dtCustomer.Rows.Count > 0)
        {
            ddlCustomer.DataSource = dtCustomer;
            ddlCustomer.DataTextField = "LNAME";
            ddlCustomer.DataValueField = "LID";
            ddlCustomer.DataBind();
            ddlCustomer.Items.Insert(0, new ListItem("--Select Customer--", "0"));
        }
    }
    private void _BindPostBillingDetails()
    {
        if (ddlFormat.SelectedValue != "0")
        {
            hidamtcess.Value = string.Empty;
            hidtdsrate.Value = string.Empty;
            hidcess1rate.Value = string.Empty;
            hidcess2rate.Value = string.Empty;
            hidsurchargerate.Value = string.Empty;
            hideffectiverate.Value = string.Empty;
            SqlParameter[] sparam = { 
                                    new SqlParameter("@FORMATID",ddlFormat.SelectedValue)
                                   };
            DataTable dtPostBilling = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_PostBilling", sparam);
            if (dtPostBilling.Rows.Count > 0)
            {
                hidamtcess.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["AMTCESS"]);
                hidtdsrate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["TDSRATE"]);
                hidcess1rate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["CESS1RATE"]);
                hidcess2rate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["CESS2RATE"]);
                hidsurchargerate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["SURCHARGERATE"]);
                hideffectiverate.Value = String.Format("{0:0.00}", dtPostBilling.Rows[0]["EFFECTIVERATE"]);

                txteffectiveratecurr.Text = String.Format("{0:0.00}", dtPostBilling.Rows[0]["EFFECTIVERATE"]);
                txteffectiveratepre.Text = String.Format("{0:0.00}", dtPostBilling.Rows[0]["EFFECTIVERATE"]);

            }
        }
    }
    protected void ddlFormat_SelectedIndex(object sender, EventArgs e)
    {
        lblMsg.Text = string.Empty;
        ClearTable();
        GetDynamicColumn();
        _BindService(ddlService1);
        _BindService(ddlService2);
        _ResetControls(tblMainInvoice);
        _ResetControls(tblcalculation);

        txtPrefix.Text = string.Empty;
        txtBillNo.Text = string.Empty;
        txtSuffix.Text = string.Empty;
        if (ddlFormat.SelectedValue != "0")
        {
            string strFinyear = string.Empty;
            if (Session["FINYEAR"] != null)
            {
                strFinyear = Session["FINYEAR"].ToString();
            }
            if (strFinyear != string.Empty)
            {
                SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TFOID",ddlFormat.SelectedValue),
                                        new SqlParameter ("@FINYEAR",strFinyear)
                                    };
                DataTable DTVoucher = DU.getdatausingprocedure("USP_IVC_GetMaxVoucherNo", sparam);
                if (DTVoucher.Rows.Count > 0)
                {
                    txtPrefix.Text = DTVoucher.Rows[0]["prefix"].ToString();
                    txtBillNo.Text = DTVoucher.Rows[0]["vouno"].ToString();
                    txtSuffix.Text = DTVoucher.Rows[0]["suffix"].ToString();
                }
            }

            _BindPostBillingDetails();
            BindCustomerPrefence();
        }
    }


    protected void ddlCustomer_SelectedIndex(object sender, EventArgs e)
    {
        txtAddress.Text = string.Empty;
        if (ddlCustomer.SelectedValue != "0")
        {
            SqlParameter[] parmaled = { 
                                    new SqlParameter("@LID",ddlCustomer.SelectedValue)
                                      };
            DataTable dtledadd = DU.getdatausingprocedure("USP_ACC_GetLedgerAddressDetails", parmaled);
            if (dtledadd.Rows.Count > 0)
            {
                txtAddress.Text = dtledadd.Rows[0]["LNAME"].ToString() + " " + dtledadd.Rows[0]["ADDRESS1"].ToString() + " " + dtledadd.Rows[0]["ADDRESS2"].ToString() + " " + dtledadd.Rows[0]["ADDRESS3"].ToString() + " " + dtledadd.Rows[0]["PLACE"].ToString() + " " + dtledadd.Rows[0]["STATENAME"].ToString();
            }
        }
        ClearTable();
        BindCustomerPrefence();
    }



    protected void ddlService_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        string ddlid = ddl.ID.ToString().Substring(10);
        int rowno = Convert.ToInt32(ddlid.ToString());
        ((TextBox)tblMainInvoice.Rows[rowno].FindControl("txtServiceChargeCtrl" + rowno)).Text = string.Empty;
        ((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidServiceCharge" + rowno)).Value = string.Empty;
        ((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = string.Empty;
        //((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = string.Empty;
        ((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidServiceIncomeLid" + rowno)).Value = string.Empty; // Service LedgerId
        SqlParameter[] parmaservice = { 
                                       new SqlParameter("@serviceid",ddl.SelectedValue)
                                      };
        DataTable dtservice = DU.getdatausingprocedure("USP_IVC_GetAllServices_By_ServiceId", parmaservice);
        if (dtservice.Rows.Count > 0)
        {
            if (Convert.ToString(dtservice.Rows[0]["TAXABILITY"]) == "0" && Convert.ToString(dtservice.Rows[0]["SUBCLASS"]) == "1")
            {
                ((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = "Taxable";
            }
            else
            {
                ((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidTaxableNonTaxable" + rowno)).Value = "NonTaxable";
            }
            if (Convert.ToString(dtservice.Rows[0]["LEDGERID"]) != string.Empty)
            {
                ((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidServiceIncomeLid" + rowno)).Value = dtservice.Rows[0]["LEDGERID"].ToString();
            }
            ((TextBox)tblMainInvoice.Rows[rowno].FindControl("txtServiceChargeCtrl" + rowno)).Text = String.Format("{0:0.00}", Convert.ToDouble(dtservice.Rows[0]["SERVICECHARGERATE"].ToString()));
            ((HiddenField)tblMainInvoice.Rows[rowno].FindControl("hidServiceCharge" + rowno)).Value = String.Format("{0:0.00}", Convert.ToDouble(dtservice.Rows[0]["SERVICECHARGERATE"].ToString()));
        }
    }

    protected void GetDynamicColumn()
    {
        SqlParameter[] sparam = { 
                                    new SqlParameter("@Formatid",ddlFormat.SelectedValue)
                                };
        DataTable DTEnableColumns = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_EnableDisable", sparam);

        if (DTEnableColumns.Rows.Count > 0)
        {
            int intRowCount = Convert.ToInt32(hidRowCount.Value.ToString());  // Table Control Count
            hidUnit.Value = string.Empty;
            hidSaleLevel.Value = string.Empty;
            hidTaxType.Value = string.Empty;
            hidDiscount.Value = string.Empty;
            hidUnitRate.Value = string.Empty;
            for (int i = 1; i <= intRowCount; i++)
            {

                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["TAXLEVEL"].ToString()) == true)
                {
                    hidSaleLevel.Value = "ItemLevel";

                    if (DTEnableColumns.Rows[0]["ITEMTAXTYPE"].ToString() == "0")
                    {
                        hidTaxType.Value = "ST"; // Service Tax
                    }
                    else if (DTEnableColumns.Rows[0]["ITEMTAXTYPE"].ToString() == "1")
                    {
                        hidTaxType.Value = "STLT"; // Service Tax,ServiceTax,Luxuary Tax
                    }
                    else if (DTEnableColumns.Rows[0]["ITEMTAXTYPE"].ToString() == "2")
                    {
                        hidTaxType.Value = "STVAT"; // Service Tax,VAT
                    }

                }
                else
                {
                    hidSaleLevel.Value = "BillLevel";
                }
                if (DTEnableColumns.Rows[0]["DISCOUNTLEVEL"].ToString() == "0")   // Apply Discount
                {
                    hidDiscount.Value = "None";
                }
                else if (DTEnableColumns.Rows[0]["DISCOUNTLEVEL"].ToString() == "1")
                {
                    hidDiscount.Value = "BillLevel";
                }
                else if (DTEnableColumns.Rows[0]["DiscountLevel"].ToString() == "2")
                {
                    hidDiscount.Value = "ItemLevel";
                }

                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["ADDDESC"].ToString()) == true)
                {
                    tdDescription.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDescriptionCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDescriptionCtrl" + i.ToString())).Style.Add("display", "inline");
                }
                else
                {
                    tdDescription.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDescriptionCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDescriptionCtrl" + i.ToString())).Style.Add("display", "none");
                }
                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["YEARREFERENCE"].ToString()) == true)
                {
                    tdYearRef.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdYearRefCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtYearRefCtrl" + i.ToString())).Style.Add("display", "inline");
                }
                else
                {
                    tdYearRef.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdYearRefCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtYearRefCtrl" + i.ToString())).Style.Add("display", "none");
                }
                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["UOM"].ToString()) == true)
                {
                    tdUom.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUomCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUomCtrl" + i.ToString())).Style.Add("display", "inline");
                }
                else
                {
                    tdUom.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUomCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUomCtrl" + i.ToString())).Style.Add("display", "none");
                }
                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["UNIT2"].ToString()) == true)
                {

                    tdUnit2.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnit2Ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit2Ctrl" + i.ToString())).Style.Add("display", "inline");

                    hidUnit.Value = "u2";
                }
                else
                {
                    tdUnit2.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnit2Ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit2Ctrl" + i.ToString())).Style.Add("display", "none");
                    hidUnit.Value = string.Empty;
                }
                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["UNIT3"].ToString()) == true)
                {
                    tdUnit3.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnit3Ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit3Ctrl" + i.ToString())).Style.Add("display", "inline");
                    hidUnit.Value = hidUnit.Value + "u3";
                }
                else
                {
                    tdUnit3.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnit3Ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit3Ctrl" + i.ToString())).Style.Add("display", "none");

                }
                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["unit4"].ToString()) == true)
                {
                    tdUnit4.Style.Add("display", "table-Cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnit4Ctrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit4Ctrl" + i.ToString())).Style.Add("display", "inline");
                    hidUnit.Value = hidUnit.Value + "u4";
                }
                else
                {
                    tdUnit4.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnit4Ctrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit4Ctrl" + i.ToString())).Style.Add("display", "none");
                }

                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["UNITRATE"].ToString()) == true)
                {
                    tdUnitRate.Style.Add("display", "table-cell");
                    tdRate.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnitRateCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdRateCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnitRateCtrl" + i.ToString())).Style.Add("display", "inline");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtRateCtrl" + i.ToString())).Style.Add("display", "inline");
                    hidUnitRate.Value = "yes";
                }
                else
                {
                    tdUnitRate.Style.Add("display", "none");
                    tdRate.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdUnitRateCtrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdRateCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnitRateCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtRateCtrl" + i.ToString())).Style.Add("display", "none");
                    hidUnitRate.Value = "No";
                }
                if (hidUnit.Value != string.Empty)
                {
                    tdRate.Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdRateCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtRateCtrl" + i.ToString())).Style.Add("display", "inline");
                }

                if (DTEnableColumns.Rows[0]["DiscountLevel"].ToString() == "1") // Discount Bill Level
                {

                    tdDiscountRate.Style.Add("display", "none");
                    tdDiscountAmt.Style.Add("display", "none");


                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDiscountRateCtrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDiscountAmtCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountRateCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountAmtCtrl" + i.ToString())).Style.Add("display", "none");

                    tddiscounttax.Style.Add("display", "table-cell");
                    tddiscounttaxper.Style.Add("display", "inline");
                    txtDiscountTaxableRate.Style.Add("display", "inline");
                    tddiscounttaxamt.Style.Add("display", "table-cell");
                    txtDiscountTaxableAmtMain.Style.Add("display", "inline");

                    tddiscountnontax.Style.Add("display", "table-cell");
                    tdnondiscountper.Style.Add("display", "table-cell");
                    txtNonTaxableDiscountRate.Style.Add("display", "inline");
                    tdnondiscounttaxamt.Style.Add("display", "table-cell");
                    txtNonTaxableDiscountAmtMain.Style.Add("display", "inline");
                }
                else if (DTEnableColumns.Rows[0]["Discountlevel"].ToString() == "2")  // Discount Item Level
                {
                    tdDiscountRate.Style.Add("display", "table-cell");
                    tdDiscountAmt.Style.Add("display", "table-cell");

                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDiscountRateCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDiscountAmtCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountRateCtrl" + i.ToString())).Style.Add("display", "inline");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountAmtCtrl" + i.ToString())).Style.Add("display", "inline");

                    tddiscounttax.Style.Add("display", "table-cell");
                    tddiscounttaxper.Style.Add("display", "table-cell");
                    tddiscounttaxamt.Style.Add("display", "table-cell");
                    txtDiscountTaxableAmtMain.Style.Add("display", "inline");
                    tddiscountnontax.Style.Add("display", "table-cell");
                    tdnondiscountper.Style.Add("display", "table-cell");
                    tdnondiscounttaxamt.Style.Add("display", "table-cell");
                    txtNonTaxableDiscountAmtMain.Style.Add("display", "inline");


                    txtNonTaxableDiscountRate.Style.Add("display", "none");
                    txtDiscountTaxableRate.Style.Add("display", "none");

                }
                else
                {

                    tdDiscountRate.Style.Add("display", "none");
                    tdDiscountAmt.Style.Add("display", "none");


                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDiscountRateCtrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdDiscountAmtCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountRateCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountAmtCtrl" + i.ToString())).Style.Add("display", "none");

                    tddiscounttax.Style.Add("display", "none");
                    tddiscounttaxper.Style.Add("display", "none");
                    txtDiscountTaxableRate.Style.Add("display", "none");
                    tddiscounttaxamt.Style.Add("display", "none");
                    txtDiscountTaxableAmtMain.Style.Add("display", "none");

                    tddiscountnontax.Style.Add("display", "none");
                    tdnondiscountper.Style.Add("display", "none");
                    txtNonTaxableDiscountRate.Style.Add("display", "none");
                    tdnondiscounttaxamt.Style.Add("display", "none");
                    txtNonTaxableDiscountAmtMain.Style.Add("display", "none");

                }
                if (DTEnableColumns.Rows[0]["ShowserviceCharge"].ToString() == "True")
                {
                    tdServiceCharge.Style.Add("display", "table-cell");
                    tdServiceChargeAmt.Style.Add("display", "table-cell");
                    tdServiceChargeTotal.Style.Add("display", "table-cell");

                    trServiceCharges.Style.Add("display", "table-row");

                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceChargeCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceChargeAmtCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceChargeTotalCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeCtrl" + i.ToString())).Style.Add("display", "inline");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeAmtCtrl" + i.ToString())).Style.Add("display", "inline");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeTotalCtrl" + i.ToString())).Style.Add("display", "inline");
                    hidSercharge.Value = "yes";
                }
                else
                {
                    tdServiceCharge.Style.Add("display", "none");
                    tdServiceChargeAmt.Style.Add("display", "none");
                    tdServiceChargeTotal.Style.Add("display", "none");

                    trServiceCharges.Style.Add("display", "none");

                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceChargeCtrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceChargeAmtCtrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceChargeTotalCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeAmtCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeTotalCtrl" + i.ToString())).Style.Add("display", "none");
                    hidSercharge.Value = "no";
                }
                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["Taxlevel"].ToString()) == true)
                {

                    tdServiceTax.Style.Add("display", "table-cell");
                    tdPECess.Style.Add("display", "table-cell");
                    tdHECess.Style.Add("display", "table-cell");


                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceTaxCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdPECessCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdHECessCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceTaxCtrl" + i.ToString())).Style.Add("display", "inline");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtPECessCtrl" + i.ToString())).Style.Add("display", "inline");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtHECessCtrl" + i.ToString())).Style.Add("display", "inline");




                    if (DTEnableColumns.Rows[0]["ItemTaxType"].ToString() == "1")
                    {

                        tdLuxury.Style.Add("display", "table-cell");
                        ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdLuxuryCtrl" + i.ToString())).Style.Add("display", "table-cell");
                        ((TextBox)tblMainInvoice.Rows[i].FindControl("txtLuxuryCtrl" + i.ToString())).Style.Add("display", "inline");
                    }
                    else if (DTEnableColumns.Rows[0]["ItemTaxType"].ToString() == "2")
                    {
                        tdVAT.Style.Add("display", "table-cell");
                        ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdVATCtrl" + i.ToString())).Style.Add("display", "table-cell");
                        ((TextBox)tblMainInvoice.Rows[i].FindControl("txtVATCtrl" + i.ToString())).Style.Add("display", "inline");
                    }
                }
                else
                {
                    tdServiceTax.Style.Add("display", "none");
                    tdPECess.Style.Add("display", "none");
                    tdHECess.Style.Add("display", "none");


                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdServiceTaxCtrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdPECessCtrl" + i.ToString())).Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdHECessCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceTaxCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtPECessCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtHECessCtrl" + i.ToString())).Style.Add("display", "none");


                    tdLuxury.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdLuxuryCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtLuxuryCtrl" + i.ToString())).Style.Add("display", "none");

                    tdVAT.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdVATCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtVATCtrl" + i.ToString())).Style.Add("display", "none");

                }
                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["Taxinclusive"].ToString()) == true || DTEnableColumns.Rows[0]["ItemTaxType"].ToString() == "0")
                {
                    tdTaxIncGrandTotal.Style.Add("display", "table-cell");

                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "table-cell");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "inline");
                    hidGrandTotal.Value = "yes";
                }
                else
                {
                    tdTaxIncGrandTotal.Style.Add("display", "none");
                    ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "none");
                    ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "none");
                    hidGrandTotal.Value = "no";
                    if (Convert.ToBoolean(DTEnableColumns.Rows[0]["UNITRATE"].ToString()) == false)
                    {
                        if (hidUnit.Value != string.Empty)
                        {

                            tdTaxIncGrandTotal.Style.Add("display", "table-cell");
                            ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "table-cell");
                            ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "inline");
                            ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).Attributes.Add("onKeyDown", "return EnabledTabKey(this.id, event);");
                            ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).CssClass = "DisableInputNumber";
                        }
                        else
                        {
                            tdTaxIncGrandTotal.Style.Add("display", "inline");
                            ((HtmlTableCell)tblMainInvoice.Rows[i].FindControl("tdTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "table-cell");
                            ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).Style.Add("display", "inline");
                            ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).Attributes.Add("onKeyDown", "return EnabledTabKey(this.id, event);");
                            ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).CssClass = "DisableInputNumber";
                        }

                    }
                }

                if (Convert.ToBoolean(DTEnableColumns.Rows[0]["NonTaxable"].ToString()) == true)
                {
                    trNonTaxable.Style.Add("display", "table-row");
                }
                else
                {
                    trNonTaxable.Style.Add("display", "none");
                }
            }

        }
    }

    protected void AddMore_Click(Object sender, EventArgs e)
    {
        ADDMoreRow();
        GetDynamicColumn();
    }

    protected void ADDMoreRow()
    {
        counter++;
        LstRowCounter.Add(counter.ToString());

        HtmlTableRow row = new HtmlTableRow();

        HtmlTableCell cell1 = new HtmlTableCell();
        cell1.ID = "tdServiceCtrl" + counter.ToString();
        cell1.Align = "Center";
        LstTdServiceCtrl.Add(cell1.ID.ToString());

        DropDownList ddlService = new DropDownList();
        ddlService.ID = "ddlService" + counter.ToString();
        ddlService.CssClass = "dropdown";
        ddlService.Width = Unit.Pixel(150);
        ddlService.AutoPostBack = true;
        _BindService(ddlService);
        ddlService.Attributes.Add("onchange", "return ValidateRequired(this.id);");
        ddlService.SelectedIndexChanged += new EventHandler(ddlService_SelectedIndexChanged);

        LstDdlService.Add(ddlService.ID.ToString());

        HiddenField hidTaxableNonTaxable = new HiddenField();
        hidTaxableNonTaxable.ID = "hidTaxableNonTaxable" + counter.ToString();
        LstHidTaxableNonTaxable.Add(hidTaxableNonTaxable.ID.ToString());

        HiddenField hidServiceIncomeLid = new HiddenField();
        hidServiceIncomeLid.ID = "hidServiceIncomeLid" + counter.ToString();
        LsthidServiceIncomeLid.Add(hidServiceIncomeLid.ID.ToString());

        HtmlTableCell cell2 = new HtmlTableCell();
        cell2.ID = "tdDescriptionCtrl" + counter.ToString();
        cell2.Align = "Center";
        cell2.Style.Add("display", "none");
        LstTdDescriptionCtrl.Add(cell2.ID.ToString());

        TextBox txtDescriptionCtrl = new TextBox();
        txtDescriptionCtrl.ID = "txtDescriptionCtrl" + counter.ToString();
        txtDescriptionCtrl.MaxLength = 248;
        txtDescriptionCtrl.Style.Add("display", "none");
        txtDescriptionCtrl.CssClass = "input";
        LstTxtDescriptionCtrl.Add(txtDescriptionCtrl.ID.ToString());

        HtmlTableCell cell3 = new HtmlTableCell();
        cell3.ID = "tdYearRefCtrl" + counter.ToString();
        cell3.Align = "Center";
        cell3.Style.Add("display", "none");
        LstTdYearRefCtrl.Add(cell3.ID.ToString());

        TextBox txtYearRefCtrl = new TextBox();
        txtYearRefCtrl.ID = "txtYearRefCtrl" + counter.ToString();
        txtYearRefCtrl.Width = Unit.Pixel(70);
        txtYearRefCtrl.MaxLength = 48;
        txtYearRefCtrl.Style.Add("display", "none");
        txtYearRefCtrl.CssClass = "input";
        LstTxtYearRefCtrl.Add(txtYearRefCtrl.ID.ToString());

        HtmlTableCell cell4 = new HtmlTableCell();
        cell4.ID = "tdUomCtrl" + counter.ToString();
        cell4.Align = "Center";
        cell4.Style.Add("display", "none");
        LstTdUomCtrl.Add(cell4.ID.ToString());

        TextBox txtUomCtrl = new TextBox();
        txtUomCtrl.ID = "txtUomCtrl" + counter.ToString();
        //txtUomCtrl.Width = Unit.Pixel(70);
        txtUomCtrl.MaxLength = 25;
        txtUomCtrl.Style.Add("display", "none");
        txtUomCtrl.CssClass = "input";
        LstTxtUomCtrl.Add(txtUomCtrl.ID.ToString());

        HtmlTableCell cell5 = new HtmlTableCell();
        cell5.ID = "tdUnit2Ctrl" + counter.ToString();
        cell5.Align = "Center";
        cell5.Style.Add("display", "none");
        LstTdUnit2Ctrl.Add(cell5.ID.ToString());

        TextBox txtUnit2Ctrl = new TextBox();
        txtUnit2Ctrl.ID = "txtUnit2Ctrl" + counter.ToString();
        txtUnit2Ctrl.Width = Unit.Pixel(70);
        txtUnit2Ctrl.MaxLength = 10;
        txtUnit2Ctrl.Style.Add("display", "none");
        txtUnit2Ctrl.CssClass = "inputnumber";
        txtUnit2Ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        LstTxtUnit2Ctrl.Add(txtUnit2Ctrl.ID.ToString());

        HtmlTableCell cell6 = new HtmlTableCell();
        cell6.ID = "tdUnit3Ctrl" + counter.ToString();
        cell6.Align = "Center";
        cell6.Style.Add("display", "none");
        LstTdUnit3Ctrl.Add(cell6.ID.ToString());

        TextBox txtUnit3Ctrl = new TextBox();
        txtUnit3Ctrl.ID = "txtUnit3Ctrl" + counter.ToString();
        txtUnit3Ctrl.Width = Unit.Pixel(70);
        txtUnit3Ctrl.MaxLength = 10;
        txtUnit3Ctrl.Style.Add("display", "none");
        txtUnit3Ctrl.CssClass = "inputnumber";
        txtUnit3Ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        LstTxtUnit3Ctrl.Add(txtUnit3Ctrl.ID.ToString());


        HtmlTableCell cell7 = new HtmlTableCell();
        cell7.ID = "tdUnit4Ctrl" + counter.ToString();
        cell7.Align = "Center";
        cell7.Style.Add("display", "none");
        LstTdUnit4Ctrl.Add(cell7.ID.ToString());

        TextBox txtUnit4Ctrl = new TextBox();
        txtUnit4Ctrl.ID = "txtUnit4Ctrl" + counter.ToString();
        txtUnit4Ctrl.Width = Unit.Pixel(70);
        txtUnit4Ctrl.MaxLength = 10;
        txtUnit4Ctrl.Style.Add("display", "none");
        txtUnit4Ctrl.CssClass = "inputnumber";
        txtUnit4Ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        LstTxtUnit4Ctrl.Add(txtUnit4Ctrl.ID.ToString());

        HtmlTableCell cell8 = new HtmlTableCell();
        cell8.ID = "tdUnitRateCtrl" + counter.ToString();
        cell8.Align = "Center";
        cell8.Style.Add("display", "none");
        LstTdUnitRateCtrl.Add(cell8.ID.ToString());

        TextBox txtUnitRateCtrl = new TextBox();
        txtUnitRateCtrl.ID = "txtUnitRateCtrl" + counter.ToString();
        txtUnitRateCtrl.Width = Unit.Pixel(70);
        txtUnitRateCtrl.MaxLength = 10;
        txtUnitRateCtrl.Style.Add("display", "none");
        txtUnitRateCtrl.CssClass = "inputnumber";
        txtUnitRateCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtUnitRateCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        LstTxtUnitRateCtrl.Add(txtUnitRateCtrl.ID.ToString());

        HtmlTableCell cell9 = new HtmlTableCell();
        cell9.ID = "tdRateCtrl" + counter.ToString();
        cell9.Align = "Center";
        cell9.Style.Add("display", "none");
        LstTdRateCtrl.Add(cell9.ID.ToString());

        TextBox txtRateCtrl = new TextBox();
        txtRateCtrl.ID = "txtRateCtrl" + counter.ToString();
        txtRateCtrl.Width = Unit.Pixel(70);
        txtRateCtrl.MaxLength = 10;
        //txtRateCtrl.Style.Add("display","none");
        txtRateCtrl.CssClass = "inputnumber";
        txtRateCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtRateCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        LstTxtRateCtrl.Add(txtRateCtrl.ID.ToString());

        HtmlTableCell cell10 = new HtmlTableCell();
        cell10.ID = "tdGrossAmtCtrl" + counter.ToString();
        cell10.Align = "Center";
        //cell10.Style.Add("display","none");
        LstTdGrossAmtCtrl.Add(cell10.ID.ToString());

        TextBox txtGrossAmtCtrl = new TextBox();
        txtGrossAmtCtrl.ID = "txtGrossAmtCtrl" + counter.ToString();
        txtGrossAmtCtrl.Width = Unit.Pixel(120);
        //txtGrossAmtCtrl.Style.Add("display","none");
        txtGrossAmtCtrl.CssClass = "inputnumber";
        txtGrossAmtCtrl.Attributes.Add("OnKeyDown", "return isNumberKey(this.id,event);");
        txtGrossAmtCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        LstTxtGrossAmtCtrl.Add(txtGrossAmtCtrl.ID.ToString());

        HiddenField hidServiceCharge = new HiddenField();
        hidServiceCharge.ID = "hidServiceCharge" + counter.ToString();
        listhidServiceCharge.Add(hidServiceCharge.ID.ToString());

        HtmlTableCell cell11 = new HtmlTableCell();
        cell11.ID = "tdDiscountRateCtrl" + counter.ToString();
        cell11.Align = "Center";
        cell11.Style.Add("display", "none");
        LstTdDiscountRateCtrl.Add(cell11.ID.ToString());

        TextBox txtDiscountRateCtrl = new TextBox();
        txtDiscountRateCtrl.ID = "txtDiscountRateCtrl" + counter.ToString();
        txtDiscountRateCtrl.Width = Unit.Pixel(80);
        txtDiscountRateCtrl.MaxLength = 5;
        txtDiscountRateCtrl.Style.Add("display", "none");
        txtDiscountRateCtrl.CssClass = "inputnumber";
        txtDiscountRateCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtDiscountRateCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        LstTxtDiscountRateCtrl.Add(txtDiscountRateCtrl.ID.ToString());

        HtmlTableCell cell12 = new HtmlTableCell();
        cell12.ID = "tdDiscountAmtCtrl" + counter.ToString();
        cell12.Align = "Center";
        cell12.Style.Add("display", "none");
        LstTdDiscountAmtCtrl.Add(cell12.ID.ToString());

        TextBox txtDiscountAmtCtrl = new TextBox();
        txtDiscountAmtCtrl.ID = "txtDiscountAmtCtrl" + counter.ToString();
        txtDiscountAmtCtrl.Width = Unit.Pixel(120);
        txtDiscountAmtCtrl.Style.Add("display", "none");
        txtDiscountAmtCtrl.CssClass = "DisableInputNumber";
        txtDiscountAmtCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtDiscountAmtCtrl.Add(txtDiscountAmtCtrl.ID.ToString());

        HtmlTableCell cell13 = new HtmlTableCell();
        cell13.ID = "tdServiceChargeCtrl" + counter.ToString();
        cell13.Align = "Center";
        cell13.Style.Add("display", "none");
        LstTdServiceChargeCtrl.Add(cell13.ID.ToString());

        TextBox txtServiceChargeCtrl = new TextBox();
        txtServiceChargeCtrl.ID = "txtServiceChargeCtrl" + counter.ToString();
        txtServiceChargeCtrl.Width = Unit.Pixel(120);
        txtServiceChargeCtrl.MaxLength = 5;
        txtServiceChargeCtrl.Style.Add("display", "none");
        txtServiceChargeCtrl.CssClass = "inputnumber";
        txtServiceChargeCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
        txtServiceChargeCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        LstTxtServiceChargeCtrl.Add(txtServiceChargeCtrl.ID.ToString());

        HtmlTableCell cell14 = new HtmlTableCell();
        cell14.ID = "tdServiceChargeAmtCtrl" + counter.ToString();
        cell14.Align = "Center";
        cell14.Style.Add("display", "none");
        LstTdServiceChargeAmtCtrl.Add(cell14.ID.ToString());

        TextBox txtServiceChargeAmtCtrl = new TextBox();
        txtServiceChargeAmtCtrl.ID = "txtServiceChargeAmtCtrl" + counter.ToString();
        txtServiceChargeAmtCtrl.Width = Unit.Pixel(100);
        txtServiceChargeAmtCtrl.MaxLength = 5;
        txtServiceChargeAmtCtrl.Style.Add("display", "none");
        txtServiceChargeAmtCtrl.CssClass = "DisableInputNumber";
        txtServiceChargeAmtCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtServiceChargeAmtCtrl.Add(txtServiceChargeAmtCtrl.ID.ToString());

        HtmlTableCell cell15 = new HtmlTableCell();
        cell15.ID = "tdServiceChargeTotalCtrl" + counter.ToString();
        cell15.Align = "Center";
        cell15.Style.Add("display", "none");
        LstTdServiceChargeTotalCtrl.Add(cell15.ID.ToString());

        TextBox txtServiceChargeTotalCtrl = new TextBox();
        txtServiceChargeTotalCtrl.ID = "txtServiceChargeTotalCtrl" + counter.ToString();
        txtServiceChargeTotalCtrl.Width = Unit.Pixel(100);
        txtServiceChargeTotalCtrl.MaxLength = 5;
        txtServiceChargeTotalCtrl.Style.Add("display", "none");
        txtServiceChargeTotalCtrl.CssClass = "DisableInputNumber";
        txtServiceChargeTotalCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtServiceChargeTotalCtrl.Add(txtServiceChargeTotalCtrl.ID.ToString());

        HtmlTableCell cell16 = new HtmlTableCell();
        cell16.ID = "tdServiceTaxCtrl" + counter.ToString();
        cell16.Align = "Center";
        cell16.Style.Add("display", "none");
        LstTdServiceTaxCtrl.Add(cell16.ID.ToString());

        TextBox txtServiceTaxCtrl = new TextBox();
        txtServiceTaxCtrl.ID = "txtServiceTaxCtrl" + counter.ToString();
        txtServiceTaxCtrl.Width = Unit.Pixel(100);
        txtServiceTaxCtrl.Style.Add("display", "none");
        txtServiceTaxCtrl.CssClass = "DisableInputNumber";
        txtServiceTaxCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtServiceTaxCtrl.Add(txtServiceTaxCtrl.ID.ToString());

        HtmlTableCell cell17 = new HtmlTableCell();
        cell17.ID = "tdPECessCtrl" + counter.ToString();
        cell17.Align = "Center";
        cell17.Style.Add("display", "none");
        LstTdPECessCtrl.Add(cell17.ID.ToString());

        TextBox txtPECessCtrl = new TextBox();
        txtPECessCtrl.ID = "txtPECessCtrl" + counter.ToString();
        txtPECessCtrl.Width = Unit.Pixel(70);
        txtPECessCtrl.Style.Add("display", "none");
        txtPECessCtrl.CssClass = "DisableInputNumber";
        txtPECessCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtPECessCtrl.Add(txtPECessCtrl.ID.ToString());

        HtmlTableCell cell18 = new HtmlTableCell();
        cell18.ID = "tdHECessCtrl" + counter.ToString();
        cell18.Align = "Center";
        cell18.Style.Add("display", "none");
        LstTdHECessCtrl.Add(cell18.ID.ToString());

        TextBox txtHECessCtrl = new TextBox();
        txtHECessCtrl.ID = "txtHECessCtrl" + counter.ToString();
        txtHECessCtrl.Width = Unit.Pixel(70);
        txtHECessCtrl.Style.Add("display", "none");
        txtHECessCtrl.CssClass = "DisableInputNumber";
        txtHECessCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtHECessCtrl.Add(txtHECessCtrl.ID.ToString());

        HtmlTableCell cell19 = new HtmlTableCell();
        cell19.ID = "tdLuxuryCtrl" + counter.ToString();
        cell19.Align = "Center";
        cell19.Style.Add("display", "none");
        LstTdLuxuryCtrl.Add(cell19.ID.ToString());

        TextBox txtLuxuryCtrl = new TextBox();
        txtLuxuryCtrl.ID = "txtLuxuryCtrl" + counter.ToString();
        txtLuxuryCtrl.Width = Unit.Pixel(70);
        txtLuxuryCtrl.Style.Add("display", "none");
        txtLuxuryCtrl.CssClass = "DisableInputNumber";
        txtLuxuryCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtLuxuryCtrl.Add(txtLuxuryCtrl.ID.ToString());

        HtmlTableCell cell20 = new HtmlTableCell();
        cell20.ID = "tdVATCtrl" + counter.ToString();
        cell20.Align = "Center";
        cell20.Style.Add("display", "none");
        LstTdVATCtrl.Add(cell20.ID.ToString());

        TextBox txtVATCtrl = new TextBox();
        txtVATCtrl.ID = "txtVATCtrl" + counter.ToString();
        txtVATCtrl.Width = Unit.Pixel(70);
        txtVATCtrl.Style.Add("display", "none");
        txtVATCtrl.CssClass = "DisableInputNumber";
        txtVATCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        LstTxtVATCtrl.Add(txtVATCtrl.ID.ToString());


        HtmlTableCell cell21 = new HtmlTableCell();
        cell21.ID = "tdTaxIncGrandTotalCtrl" + counter.ToString();
        cell21.Align = "Center";
        cell21.Style.Add("display", "none");
        LstTdTaxIncGrandTotalCtrl.Add(cell21.ID.ToString());

        TextBox txtTaxIncGrandTotalCtrl = new TextBox();
        txtTaxIncGrandTotalCtrl.ID = "txtTaxIncGrandTotalCtrl" + counter.ToString();
        txtTaxIncGrandTotalCtrl.Width = Unit.Pixel(120);
        txtTaxIncGrandTotalCtrl.Style.Add("display", "none");
        txtTaxIncGrandTotalCtrl.CssClass = "DisableInputNumber";
        txtTaxIncGrandTotalCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
        txtTaxIncGrandTotalCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
        LstTxtTaxIncGrandTotalCtrl.Add(txtTaxIncGrandTotalCtrl.ID.ToString());

        cell1.Controls.Add(ddlService);
        cell1.Controls.Add(hidTaxableNonTaxable);
        cell1.Controls.Add(hidServiceIncomeLid);
        cell2.Controls.Add(txtDescriptionCtrl);
        cell3.Controls.Add(txtYearRefCtrl);
        cell4.Controls.Add(txtUomCtrl);
        cell5.Controls.Add(txtUnit2Ctrl);
        cell6.Controls.Add(txtUnit3Ctrl);
        cell7.Controls.Add(txtUnit4Ctrl);
        cell8.Controls.Add(txtUnitRateCtrl);
        cell9.Controls.Add(txtRateCtrl);
        cell10.Controls.Add(txtGrossAmtCtrl);
        cell10.Controls.Add(hidServiceCharge);
        cell11.Controls.Add(txtDiscountRateCtrl);
        cell12.Controls.Add(txtDiscountAmtCtrl);
        cell13.Controls.Add(txtServiceChargeCtrl);
        cell14.Controls.Add(txtServiceChargeAmtCtrl);
        cell15.Controls.Add(txtServiceChargeTotalCtrl);
        cell16.Controls.Add(txtServiceTaxCtrl);
        cell17.Controls.Add(txtPECessCtrl);
        cell18.Controls.Add(txtHECessCtrl);
        cell19.Controls.Add(txtLuxuryCtrl);
        cell20.Controls.Add(txtVATCtrl);
        cell21.Controls.Add(txtTaxIncGrandTotalCtrl);

        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        row.Cells.Add(cell3);
        row.Cells.Add(cell4);
        row.Cells.Add(cell5);
        row.Cells.Add(cell6);
        row.Cells.Add(cell7);
        row.Cells.Add(cell8);
        row.Cells.Add(cell9);
        row.Cells.Add(cell10);
        row.Cells.Add(cell11);
        row.Cells.Add(cell12);
        row.Cells.Add(cell13);
        row.Cells.Add(cell14);
        row.Cells.Add(cell15);
        row.Cells.Add(cell16);
        row.Cells.Add(cell17);
        row.Cells.Add(cell18);
        row.Cells.Add(cell19);
        row.Cells.Add(cell20);
        row.Cells.Add(cell21);

        tblMainInvoice.Rows.Add(row);

        hidRowCount.Value = counter.ToString();

        ViewState["LstRowCounter"] = LstRowCounter;

        ViewState["LstTdServiceCtrl"] = LstTdServiceCtrl;
        ViewState["LstDdlService"] = LstDdlService;
        ViewState["LstHidTaxableNonTaxable"] = LstHidTaxableNonTaxable;
        ViewState["LsthidServiceIncomeLid"] = LsthidServiceIncomeLid;

        ViewState["LstTdDescriptionCtrl"] = LstTdDescriptionCtrl;
        ViewState["LstTxtDescriptionCtrl"] = LstTxtDescriptionCtrl;

        ViewState["LstTdYearRefCtrl"] = LstTdYearRefCtrl;
        ViewState["LstTxtYearRefCtrl"] = LstTxtYearRefCtrl;

        ViewState["LstTdUomCtrl"] = LstTdUomCtrl;
        ViewState["LstTxtUomCtrl"] = LstTxtUomCtrl;

        ViewState["LstTdUnit2Ctrl"] = LstTdUnit2Ctrl;
        ViewState["LstTxtUnit2Ctrl"] = LstTxtUnit2Ctrl;
        ViewState["LstTdUnit3Ctrl"] = LstTdUnit3Ctrl;
        ViewState["LstTxtUnit3Ctrl"] = LstTxtUnit3Ctrl;
        ViewState["LstTdUnit4Ctrl"] = LstTdUnit4Ctrl;
        ViewState["LstTxtUnit4Ctrl"] = LstTxtUnit4Ctrl;

        ViewState["LstTdUnitRateCtrl"] = LstTdUnitRateCtrl;
        ViewState["LstTxtUnitRateCtrl"] = LstTxtUnitRateCtrl;
        ViewState["LstTdRateCtrl"] = LstTdRateCtrl;
        ViewState["LstTxtRateCtrl"] = LstTxtRateCtrl;

        ViewState["LstTdGrossAmtCtrl"] = LstTdGrossAmtCtrl;
        ViewState["LstTxtGrossAmtCtrl"] = LstTxtGrossAmtCtrl;

        ViewState["LstTdDiscountRateCtrl"] = LstTdDiscountRateCtrl;
        ViewState["LstTxtDiscountRateCtrl"] = LstTxtDiscountRateCtrl;

        ViewState["LstTdDiscountAmtCtrl"] = LstTdDiscountAmtCtrl;
        ViewState["LstTxtDiscountAmtCtrl"] = LstTxtDiscountAmtCtrl;

        ViewState["LstTdServiceChargeCtrl"] = LstTdServiceChargeCtrl;
        ViewState["listhidServiceCharge"] = listhidServiceCharge;
        ViewState["LstTxtServiceChargeCtrl"] = LstTxtServiceChargeCtrl;
        ViewState["LstTdServiceChargeAmtCtrl"] = LstTdServiceChargeAmtCtrl;
        ViewState["LstTxtServiceChargeAmtCtrl"] = LstTxtServiceChargeAmtCtrl;
        ViewState["LstTdServiceChargeTotalCtrl"] = LstTdServiceChargeTotalCtrl;
        ViewState["LstTxtServiceChargeTotalCtrl"] = LstTxtServiceChargeTotalCtrl;

        ViewState["LstTdServiceTaxCtrl"] = LstTdServiceTaxCtrl;
        ViewState["LstTxtServiceTaxCtrl"] = LstTxtServiceTaxCtrl;
        ViewState["LstTdPECessCtrl"] = LstTdPECessCtrl;
        ViewState["LstTxtPECessCtrl"] = LstTxtPECessCtrl;
        ViewState["LstTdHECessCtrl"] = LstTdHECessCtrl;
        ViewState["LstTxtHECessCtrl"] = LstTxtHECessCtrl;
        ViewState["LstTdLuxuryCtrl"] = LstTdLuxuryCtrl;
        ViewState["LstTxtLuxuryCtrl"] = LstTxtLuxuryCtrl;
        ViewState["LstTdVATCtrl"] = LstTdVATCtrl;
        ViewState["LstTxtVATCtrl"] = LstTxtVATCtrl;

        ViewState["LstTdTaxIncGrandTotalCtrl"] = LstTdTaxIncGrandTotalCtrl;
        ViewState["LstTxtTaxIncGrandTotalCtrl"] = LstTxtTaxIncGrandTotalCtrl;

    }
    protected void OverrideAddMore()
    {

        LstRowCounter = (List<string>)ViewState["LstRowCounter"];

        LstTdServiceCtrl = (List<string>)ViewState["LstTdServiceCtrl"];
        LstDdlService = (List<string>)ViewState["LstDdlService"];

        LstHidTaxableNonTaxable = (List<string>)ViewState["LstHidTaxableNonTaxable"];
        LsthidServiceIncomeLid = (List<string>)ViewState["LsthidServiceIncomeLid"];

        LstTdDescriptionCtrl = (List<string>)ViewState["LstTdDescriptionCtrl"];
        LstTxtDescriptionCtrl = (List<string>)ViewState["LstTxtDescriptionCtrl"];

        LstTdYearRefCtrl = (List<string>)ViewState["LstTdYearRefCtrl"];
        LstTxtYearRefCtrl = (List<string>)ViewState["LstTxtYearRefCtrl"];

        LstTdUomCtrl = (List<string>)ViewState["LstTdUomCtrl"];
        LstTxtUomCtrl = (List<string>)ViewState["LstTxtUomCtrl"];

        LstTdUnit2Ctrl = (List<string>)ViewState["LstTdUnit2Ctrl"];
        LstTxtUnit2Ctrl = (List<string>)ViewState["LstTxtUnit2Ctrl"];
        LstTdUnit3Ctrl = (List<string>)ViewState["LstTdUnit3Ctrl"];
        LstTxtUnit3Ctrl = (List<string>)ViewState["LstTxtUnit3Ctrl"];
        LstTdUnit4Ctrl = (List<string>)ViewState["LstTdUnit4Ctrl"];
        LstTxtUnit4Ctrl = (List<string>)ViewState["LstTxtUnit4Ctrl"];

        LstTdUnitRateCtrl = (List<string>)ViewState["LstTdUnitRateCtrl"];
        LstTxtUnitRateCtrl = (List<string>)ViewState["LstTxtUnitRateCtrl"];
        LstTdRateCtrl = (List<string>)ViewState["LstTdRateCtrl"];
        LstTxtRateCtrl = (List<string>)ViewState["LstTxtRateCtrl"];

        LstTdGrossAmtCtrl = (List<string>)ViewState["LstTdGrossAmtCtrl"];
        LstTxtGrossAmtCtrl = (List<string>)ViewState["LstTxtGrossAmtCtrl"];

        LstTdDiscountRateCtrl = (List<string>)ViewState["LstTdDiscountRateCtrl"];
        LstTxtDiscountRateCtrl = (List<string>)ViewState["LstTxtDiscountRateCtrl"];

        LstTdDiscountAmtCtrl = (List<string>)ViewState["LstTdDiscountAmtCtrl"];
        LstTxtDiscountAmtCtrl = (List<string>)ViewState["LstTxtDiscountAmtCtrl"];

        LstTdServiceChargeCtrl = (List<string>)ViewState["LstTdServiceChargeCtrl"];
        listhidServiceCharge = (List<string>)ViewState["listhidServiceCharge"];
        LstTxtServiceChargeCtrl = (List<string>)ViewState["LstTxtServiceChargeCtrl"];
        LstTdServiceChargeAmtCtrl = (List<string>)ViewState["LstTdServiceChargeAmtCtrl"];
        LstTxtServiceChargeAmtCtrl = (List<string>)ViewState["LstTxtServiceChargeAmtCtrl"];
        LstTdServiceChargeTotalCtrl = (List<string>)ViewState["LstTdServiceChargeTotalCtrl"];
        LstTxtServiceChargeTotalCtrl = (List<string>)ViewState["LstTxtServiceChargeTotalCtrl"];

        LstTdServiceTaxCtrl = (List<string>)ViewState["LstTdServiceTaxCtrl"];
        LstTxtServiceTaxCtrl = (List<string>)ViewState["LstTxtServiceTaxCtrl"];
        LstTdPECessCtrl = (List<string>)ViewState["LstTdPECessCtrl"];
        LstTxtPECessCtrl = (List<string>)ViewState["LstTxtPECessCtrl"];
        LstTdHECessCtrl = (List<string>)ViewState["LstTdHECessCtrl"];
        LstTxtHECessCtrl = (List<string>)ViewState["LstTxtHECessCtrl"];
        LstTdLuxuryCtrl = (List<string>)ViewState["LstTdLuxuryCtrl"];
        LstTxtLuxuryCtrl = (List<string>)ViewState["LstTxtLuxuryCtrl"];
        LstTdVATCtrl = (List<string>)ViewState["LstTdVATCtrl"];
        LstTxtVATCtrl = (List<string>)ViewState["LstTxtVATCtrl"];

        LstTdTaxIncGrandTotalCtrl = (List<string>)ViewState["LstTdTaxIncGrandTotalCtrl"];
        LstTxtTaxIncGrandTotalCtrl = (List<string>)ViewState["LstTxtTaxIncGrandTotalCtrl"];

        if (LstRowCounter != null)
        {
            int tempcounter = Convert.ToInt32(LstRowCounter.Count);
            for (int i = 0; i < tempcounter; i++)
            {
                counter++;
                //     LstRowCounter.Add(counter.ToString());

                HtmlTableRow row = new HtmlTableRow();

                HtmlTableCell cell1 = new HtmlTableCell();
                cell1.ID = LstTdServiceCtrl[i].ToString();
                cell1.Align = "Center";

                DropDownList ddlService = new DropDownList();
                ddlService.ID = LstDdlService[i].ToString();
                ddlService.CssClass = "dropdown";
                ddlService.Width = Unit.Pixel(150);
                _BindService(ddlService);
                ddlService.AutoPostBack = true;
                ddlService.Attributes.Add("onchange", "return ValidateRequired(this.id);");
                ddlService.SelectedIndexChanged += new EventHandler(ddlService_SelectedIndexChanged);

                HiddenField hidTaxableNonTaxable = new HiddenField();
                hidTaxableNonTaxable.ID = LstHidTaxableNonTaxable[i].ToString();

                HiddenField hidServiceIncomeLid = new HiddenField();
                hidServiceIncomeLid.ID = LsthidServiceIncomeLid[i].ToString();

                HtmlTableCell cell2 = new HtmlTableCell();
                cell2.ID = LstTdDescriptionCtrl[i].ToString();
                cell2.Align = "Center";
                cell2.Style.Add("display", "none");


                TextBox txtDescriptionCtrl = new TextBox();
                txtDescriptionCtrl.ID = LstTxtDescriptionCtrl[i].ToString();
                txtDescriptionCtrl.MaxLength = 248;
                txtDescriptionCtrl.Style.Add("display", "none");
                txtDescriptionCtrl.CssClass = "input";

                HtmlTableCell cell3 = new HtmlTableCell();
                cell3.ID = LstTdYearRefCtrl[i].ToString();
                cell3.Align = "Center";
                cell3.Style.Add("display", "none");

                TextBox txtYearRefCtrl = new TextBox();
                txtYearRefCtrl.ID = LstTxtYearRefCtrl[i].ToString();
                txtYearRefCtrl.MaxLength = 48;
                txtYearRefCtrl.Width = Unit.Pixel(70);
                txtYearRefCtrl.Style.Add("display", "none");
                txtYearRefCtrl.CssClass = "input";

                HtmlTableCell cell4 = new HtmlTableCell();
                cell4.ID = LstTdUomCtrl[i].ToString();
                cell4.Align = "Center";
                cell4.Style.Add("display", "none");

                TextBox txtUomCtrl = new TextBox();
                txtUomCtrl.ID = LstTxtUomCtrl[i].ToString();
                //txtUomCtrl.Width = Unit.Pixel(70);
                txtUomCtrl.Style.Add("display", "none");
                txtUomCtrl.CssClass = "input";

                HtmlTableCell cell5 = new HtmlTableCell();
                cell5.ID = LstTdUnit2Ctrl[i].ToString();
                cell5.Align = "Center";
                cell5.Style.Add("display", "none");

                TextBox txtUnit2Ctrl = new TextBox();
                txtUnit2Ctrl.ID = LstTxtUnit2Ctrl[i].ToString();
                txtUnit2Ctrl.Width = Unit.Pixel(70);
                txtUnit2Ctrl.MaxLength = 10;
                txtUnit2Ctrl.Style.Add("display", "none");
                txtUnit2Ctrl.CssClass = "inputnumber";
                txtUnit2Ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell6 = new HtmlTableCell();
                cell6.ID = LstTdUnit3Ctrl[i].ToString();
                cell6.Align = "Center";
                cell6.Style.Add("display", "none");

                TextBox txtUnit3Ctrl = new TextBox();
                txtUnit3Ctrl.ID = LstTxtUnit3Ctrl[i].ToString();
                txtUnit3Ctrl.Width = Unit.Pixel(70);
                txtUnit3Ctrl.MaxLength = 10;
                txtUnit3Ctrl.Style.Add("display", "none");
                txtUnit3Ctrl.CssClass = "inputnumber";
                txtUnit3Ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell7 = new HtmlTableCell();
                cell7.ID = LstTdUnit4Ctrl[i].ToString();
                cell7.Align = "Center";
                cell7.Style.Add("display", "none");

                TextBox txtUnit4Ctrl = new TextBox();
                txtUnit4Ctrl.ID = LstTxtUnit4Ctrl[i].ToString();
                txtUnit4Ctrl.Width = Unit.Pixel(70);
                txtUnit4Ctrl.MaxLength = 10;
                txtUnit4Ctrl.Style.Add("display", "none");
                txtUnit4Ctrl.CssClass = "inputnumber";
                txtUnit4Ctrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell8 = new HtmlTableCell();
                cell8.ID = LstTdUnitRateCtrl[i].ToString();
                cell8.Align = "Center";
                cell8.Style.Add("display", "none");

                TextBox txtUnitRateCtrl = new TextBox();
                txtUnitRateCtrl.ID = LstTxtUnitRateCtrl[i].ToString();
                txtUnitRateCtrl.Width = Unit.Pixel(70);
                txtUnitRateCtrl.MaxLength = 10;
                txtUnitRateCtrl.Style.Add("display", "none");
                txtUnitRateCtrl.CssClass = "inputnumber";
                txtUnitRateCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell9 = new HtmlTableCell();
                cell9.ID = LstTdRateCtrl[i].ToString();
                cell9.Align = "Center";
                cell9.Style.Add("display", "none");

                TextBox txtRateCtrl = new TextBox();
                txtRateCtrl.ID = LstTxtRateCtrl[i].ToString();
                txtRateCtrl.Width = Unit.Pixel(70);
                txtRateCtrl.MaxLength = 10;
                txtRateCtrl.Style.Add("display", "none");
                txtRateCtrl.CssClass = "inputnumber";
                txtRateCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
                txtRateCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");

                HtmlTableCell cell10 = new HtmlTableCell();
                cell10.ID = LstTdGrossAmtCtrl[i].ToString();
                cell10.Align = "Center";
                //cell10.style.Add("display","none");

                TextBox txtGrossAmtCtrl = new TextBox();
                txtGrossAmtCtrl.ID = LstTxtGrossAmtCtrl[i].ToString();
                txtGrossAmtCtrl.Width = Unit.Pixel(120);
                //txtGrossAmtCtrl.style.Add("display","none");
                txtGrossAmtCtrl.CssClass = "inputnumber";
                txtGrossAmtCtrl.Attributes.Add("OnKeyDown", "return isNumberKey(this.id,event);");
                txtGrossAmtCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");

                HiddenField hidServiceCharge = new HiddenField();
                hidServiceCharge.ID = listhidServiceCharge[i].ToString();

                HtmlTableCell cell11 = new HtmlTableCell();
                cell11.ID = LstTdDiscountRateCtrl[i].ToString();
                cell11.Align = "Center";
                cell11.Style.Add("display", "none");

                TextBox txtDiscountRateCtrl = new TextBox();
                txtDiscountRateCtrl.ID = LstTxtDiscountRateCtrl[i].ToString();
                txtDiscountRateCtrl.Width = Unit.Pixel(80);
                txtDiscountRateCtrl.MaxLength = 5;
                txtDiscountRateCtrl.Style.Add("display", "none");
                txtDiscountRateCtrl.CssClass = "inputnumber";
                txtDiscountRateCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");

                HtmlTableCell cell12 = new HtmlTableCell();
                cell12.ID = LstTdDiscountAmtCtrl[i].ToString();
                cell12.Align = "Center";
                cell12.Style.Add("display", "none");

                TextBox txtDiscountAmtCtrl = new TextBox();
                txtDiscountAmtCtrl.ID = LstTxtDiscountAmtCtrl[i].ToString();
                txtDiscountAmtCtrl.Width = Unit.Pixel(120);
                txtDiscountAmtCtrl.Style.Add("display", "none");
                txtDiscountAmtCtrl.CssClass = "DisableInputNumber";
                txtDiscountAmtCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell13 = new HtmlTableCell();
                cell13.ID = LstTdServiceChargeCtrl[i].ToString();
                cell13.Align = "Center";
                cell13.Style.Add("display", "none");

                TextBox txtServiceChargeCtrl = new TextBox();
                txtServiceChargeCtrl.ID = LstTxtServiceChargeCtrl[i].ToString();
                txtServiceChargeCtrl.Width = Unit.Pixel(120);
                txtServiceChargeCtrl.MaxLength = 5;
                txtServiceChargeCtrl.Style.Add("display", "none");
                txtServiceChargeCtrl.CssClass = "inputnumber";
                txtServiceChargeCtrl.Attributes.Add("OnKeyPress", "return isNumberKey(this.id,event);");
                txtServiceChargeCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");


                HtmlTableCell cell14 = new HtmlTableCell();
                cell14.ID = LstTdServiceChargeAmtCtrl[i].ToString();
                cell14.Align = "Center";
                cell14.Style.Add("display", "none");

                TextBox txtServiceChargeAmtCtrl = new TextBox();
                txtServiceChargeAmtCtrl.ID = LstTxtServiceChargeAmtCtrl[i].ToString();
                txtServiceChargeAmtCtrl.Width = Unit.Pixel(100);
                txtServiceChargeAmtCtrl.MaxLength = 5;
                txtServiceChargeAmtCtrl.Style.Add("display", "none");
                txtServiceChargeAmtCtrl.CssClass = "DisableInputNumber";
                txtServiceChargeAmtCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell15 = new HtmlTableCell();
                cell15.ID = LstTdServiceChargeTotalCtrl[i].ToString();
                cell15.Align = "Center";
                cell15.Style.Add("display", "none");

                TextBox txtServiceChargeTotalCtrl = new TextBox();
                txtServiceChargeTotalCtrl.ID = LstTxtServiceChargeTotalCtrl[i].ToString();
                txtServiceChargeTotalCtrl.Width = Unit.Pixel(100);
                txtServiceChargeTotalCtrl.MaxLength = 5;
                txtServiceChargeTotalCtrl.Style.Add("display", "none");
                txtServiceChargeTotalCtrl.CssClass = "DisableInputNumber";
                txtServiceChargeTotalCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell16 = new HtmlTableCell();
                cell16.ID = LstTdServiceTaxCtrl[i].ToString();
                cell16.Align = "Center";
                cell16.Style.Add("display", "none");

                TextBox txtServiceTaxCtrl = new TextBox();
                txtServiceTaxCtrl.ID = LstTxtServiceTaxCtrl[i].ToString();
                txtServiceTaxCtrl.Width = Unit.Pixel(100);
                txtServiceTaxCtrl.Style.Add("display", "none");
                txtServiceTaxCtrl.CssClass = "DisableInputNumber";
                txtServiceTaxCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell17 = new HtmlTableCell();
                cell17.ID = LstTdPECessCtrl[i].ToString();
                cell17.Align = "Center";
                cell17.Style.Add("display", "none");

                TextBox txtPECessCtrl = new TextBox();
                txtPECessCtrl.ID = LstTxtPECessCtrl[i].ToString();
                txtPECessCtrl.Width = Unit.Pixel(70);
                txtPECessCtrl.Style.Add("display", "none");
                txtPECessCtrl.CssClass = "DisableInputNumber";
                txtPECessCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell18 = new HtmlTableCell();
                cell18.ID = LstTdHECessCtrl[i].ToString();
                cell18.Align = "Center";
                cell18.Style.Add("display", "none");

                TextBox txtHECessCtrl = new TextBox();
                txtHECessCtrl.ID = LstTxtHECessCtrl[i].ToString();
                txtHECessCtrl.Width = Unit.Pixel(70);
                txtHECessCtrl.Style.Add("display", "none");
                txtHECessCtrl.CssClass = "DisableInputNumber";
                txtHECessCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell19 = new HtmlTableCell();
                cell19.ID = LstTdLuxuryCtrl[i].ToString();
                cell19.Align = "Center";
                cell19.Style.Add("display", "none");

                TextBox txtLuxuryCtrl = new TextBox();
                txtLuxuryCtrl.ID = LstTxtLuxuryCtrl[i].ToString();
                txtLuxuryCtrl.Width = Unit.Pixel(70);
                txtLuxuryCtrl.Style.Add("display", "none");
                txtLuxuryCtrl.CssClass = "DisableInputNumber";
                txtLuxuryCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell20 = new HtmlTableCell();
                cell20.ID = LstTdVATCtrl[i].ToString();
                cell20.Align = "Center";
                cell20.Style.Add("display", "none");

                TextBox txtVATCtrl = new TextBox();
                txtVATCtrl.ID = LstTxtVATCtrl[i].ToString();
                txtVATCtrl.Width = Unit.Pixel(70);
                txtVATCtrl.Style.Add("display", "none");
                txtVATCtrl.CssClass = "DisableInputNumber";
                txtVATCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");

                HtmlTableCell cell21 = new HtmlTableCell();
                cell21.ID = LstTdTaxIncGrandTotalCtrl[i].ToString();
                cell21.Align = "Center";
                cell21.Style.Add("display", "none");

                TextBox txtTaxIncGrandTotalCtrl = new TextBox();
                txtTaxIncGrandTotalCtrl.ID = LstTxtTaxIncGrandTotalCtrl[i].ToString();
                txtTaxIncGrandTotalCtrl.Width = Unit.Pixel(120);
                txtTaxIncGrandTotalCtrl.Style.Add("display", "none");
                txtTaxIncGrandTotalCtrl.Attributes.Add("OnKeyDown", "return EnabledTabKey(this.id,event);");
                txtTaxIncGrandTotalCtrl.Attributes.Add("onBlur", "return CalculateGrandTotal(this.id);");
                txtTaxIncGrandTotalCtrl.CssClass = "DisableInputNumber";

                cell1.Controls.Add(ddlService);
                cell1.Controls.Add(hidTaxableNonTaxable);
                cell1.Controls.Add(hidServiceIncomeLid);
                cell2.Controls.Add(txtDescriptionCtrl);
                cell3.Controls.Add(txtYearRefCtrl);
                cell4.Controls.Add(txtUomCtrl);
                cell5.Controls.Add(txtUnit2Ctrl);
                cell6.Controls.Add(txtUnit3Ctrl);
                cell7.Controls.Add(txtUnit4Ctrl);
                cell8.Controls.Add(txtUnitRateCtrl);
                cell9.Controls.Add(txtRateCtrl);
                cell10.Controls.Add(txtGrossAmtCtrl);
                cell10.Controls.Add(hidServiceCharge);
                cell11.Controls.Add(txtDiscountRateCtrl);
                cell12.Controls.Add(txtDiscountAmtCtrl);
                cell13.Controls.Add(txtServiceChargeCtrl);
                cell14.Controls.Add(txtServiceChargeAmtCtrl);
                cell15.Controls.Add(txtServiceChargeTotalCtrl);
                cell16.Controls.Add(txtServiceTaxCtrl);
                cell17.Controls.Add(txtPECessCtrl);
                cell18.Controls.Add(txtHECessCtrl);
                cell19.Controls.Add(txtLuxuryCtrl);
                cell20.Controls.Add(txtVATCtrl);
                cell21.Controls.Add(txtTaxIncGrandTotalCtrl);

                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                row.Cells.Add(cell3);
                row.Cells.Add(cell4);
                row.Cells.Add(cell5);
                row.Cells.Add(cell6);
                row.Cells.Add(cell7);
                row.Cells.Add(cell8);
                row.Cells.Add(cell9);
                row.Cells.Add(cell10);
                row.Cells.Add(cell11);
                row.Cells.Add(cell12);
                row.Cells.Add(cell13);
                row.Cells.Add(cell14);
                row.Cells.Add(cell15);
                row.Cells.Add(cell16);
                row.Cells.Add(cell17);
                row.Cells.Add(cell18);
                row.Cells.Add(cell19);
                row.Cells.Add(cell20);
                row.Cells.Add(cell21);

                tblMainInvoice.Rows.Add(row);
            }

        }
    }

    private void _GetCurrentServiceTaxRate(string strCurrentDate)
    {
        if (strCurrentDate.ToString() != string.Empty)
        {
            string[] strArray = strCurrentDate.Split('/');
            strCurrentDate = strArray[1] + "/" + strArray[0] + "/" + strArray[2].ToString();
            SqlParameter[] sparam = { 
                                   new SqlParameter("@CURRENTDATE",strCurrentDate)
                                 };
            DataTable dtSerTax = DU.getdatausingprocedure("USP_IVC_GetCurrentServiceTaxRate", sparam);
            if (dtSerTax.Rows.Count > 0)
            {
                txtServiceTaxRateMain.Text = dtSerTax.Rows[0]["SERVICETAX"].ToString();
                hidServiceTaxLid.Value = dtSerTax.Rows[0]["SERVICETAX_LID"].ToString();
                txtPECessRateMain.Text = dtSerTax.Rows[0]["PCESS"].ToString();
                hidPECessLid.Value = dtSerTax.Rows[0]["PCESS_LID"].ToString();
                txtHECessRateMain.Text = dtSerTax.Rows[0]["HCESS"].ToString();
                hidHECessLid.Value = dtSerTax.Rows[0]["HCESS_LID"].ToString();
            }
        }
    }
    protected void txtDate_TextChanged(object sender, EventArgs e)
    {
        _GetCurrentServiceTaxRate(txtDate.Text);
    }

    protected void BindCustomerPrefence()
    {
        if (ddlCustomer.SelectedValue != "0" && ddlFormat.SelectedValue != "0")
        {

            SqlParameter[] parma = {       //// Get CustomerPreferenceid and Credit Days for PreLoad Rows by Customer PreferenceCild Table
                                new SqlParameter("@CUSTOMERLEDGERID",ddlCustomer.SelectedValue),
                                new SqlParameter("@FORMATID",ddlFormat.SelectedValue)
                               };
            DataTable dtCustomerprefid = DU.getdatausingprocedure("USP_IVC_GetCustomerPreference", parma);
            if (dtCustomerprefid.Rows.Count > 0)
            {
                if (Convert.ToInt32(dtCustomerprefid.Rows[0]["CUSTOMERPREID"]) > 0)
                {

                    hidCustomerprefid.Value = dtCustomerprefid.Rows[0]["CUSTOMERPREID"].ToString();
                    if (dtCustomerprefid.Rows[0]["CRDAYS"].ToString() != string.Empty)
                    {
                        txtCreditDays.Text = dtCustomerprefid.Rows[0]["CRDAYS"].ToString();
                    }
                    else
                    {
                        txtCreditDays.Text = "0";
                    }
                }
            }
            if (hidCustomerprefid.Value != string.Empty)
            {
                SqlParameter[] parmaa = { 
                                 new SqlParameter("@CUSTOMERPREID",hidCustomerprefid.Value)
                                    };
                DataTable dtPreLoadRows = DU.getdatausingprocedure("USP_IVC_GetCustomerPreferenceServices", parmaa);
                if (dtPreLoadRows.Rows.Count > 0)
                {
                    if (dtPreLoadRows.Rows.Count > 2)
                    {
                        for (int i = 2; i < dtPreLoadRows.Rows.Count; i++)
                        {
                            ADDMoreRow();
                        }
                    }
                    GetDynamicColumn();
                    BindServiceRows(dtPreLoadRows);
                }
            }
        }
    }
    protected void BindServiceRows(DataTable dtPreLoadRows)
    {
        SqlParameter[] parma = { 
                                new SqlParameter("@Formatid",ddlFormat.SelectedValue)
                               };
        DataTable DTEnableColumns = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_EnableDisable", parma);
        if (dtPreLoadRows.Rows.Count > 0)
        {
            grid.DataSource = dtPreLoadRows;
            grid.DataBind();
            for (int i = 1; i <= dtPreLoadRows.Rows.Count; i++)
            {
                DropDownList ddlService = (DropDownList)tblMainInvoice.Rows[i].FindControl("ddlService" + i.ToString());
                ddlService.SelectedValue = Convert.ToString(dtPreLoadRows.Rows[i - 1]["SERVICEID"]);
                ddlService_SelectedIndexChanged(ddlService, EventArgs.Empty);

                HiddenField hidTaxableNonTaxable = (HiddenField)tblMainInvoice.Rows[i].FindControl("hidTaxableNonTaxable" + i.ToString());
                if (DTEnableColumns.Rows[0]["Adddesc"].ToString() == "True")
                {
                    TextBox txtDescriptionCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtDescriptionCtrl" + i.ToString());
                }
                if (DTEnableColumns.Rows[0]["Yearreference"].ToString() == "True")
                {
                    TextBox txtYearRefCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtYearRefCtrl" + i.ToString());
                }
                if (DTEnableColumns.Rows[0]["uom"].ToString() == "True")
                {
                    TextBox txtUomCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtUomCtrl" + i.ToString());
                    txtUomCtrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UOM"]);
                }
                if (DTEnableColumns.Rows[0]["unit2"].ToString() == "True")
                {
                    TextBox txtUnit2Ctrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit2Ctrl" + i.ToString());
                    txtUnit2Ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT2"]);
                }
                if (DTEnableColumns.Rows[0]["unit3"].ToString() == "True")
                {
                    TextBox txtUnit3Ctrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit3Ctrl" + i.ToString());
                    txtUnit3Ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT3"]);
                }
                if (DTEnableColumns.Rows[0]["unit4"].ToString() == "True")
                {
                    TextBox txtUnit4Ctrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit4Ctrl" + i.ToString());
                    txtUnit4Ctrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT4"]);
                }
                if (DTEnableColumns.Rows[0]["unitrate"].ToString() == "True")
                {
                    TextBox txtUnitRateCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtUnitRateCtrl" + i.ToString());

                    txtUnitRateCtrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["UNIT"]);

                    TextBox txtRateCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtRateCtrl" + i.ToString());

                    txtRateCtrl.Text = Convert.ToString(dtPreLoadRows.Rows[i - 1]["RATE"]);

                }
                TextBox txtGrossAmtCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtGrossAmtCtrl" + i.ToString());

                ScriptManager.RegisterStartupScript(this, this.GetType(), "script" + i.ToString() + "", "CalculateGrandTotal('" + txtGrossAmtCtrl.ID + "');", true);

                if (DTEnableColumns.Rows[0]["Discountlevel"].ToString() == "2")
                {
                    TextBox txtDiscountRateCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountRateCtrl" + i.ToString());

                    TextBox txtDiscountAmtCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountAmtCtrl" + i.ToString());

                }
                if (DTEnableColumns.Rows[0]["ShowserviceCharge"].ToString() == "True")
                {
                    TextBox txtServiceChargeCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeCtrl" + i.ToString());


                    TextBox txtServiceChargeAmtCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeAmtCtrl" + i.ToString());
                    TextBox txtServiceChargeTotalCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeTotalCtrl" + i.ToString());

                }
                if (DTEnableColumns.Rows[0]["Taxlevel"].ToString() == "True")
                {
                    TextBox txtServiceTaxCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceTaxCtrl" + i.ToString());
                    TextBox txtPECessCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtPECessCtrl" + i.ToString());
                    TextBox txtHECessCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtHECessCtrl" + i.ToString());
                    if (DTEnableColumns.Rows[0]["ItemTaxType"].ToString() == "1")
                    {
                        TextBox txtLuxuryCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtLuxuryCtrl" + i.ToString());
                    }
                    else if (DTEnableColumns.Rows[0]["ItemTaxType"].ToString() == "2")
                    {
                        TextBox txtVATCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtVATCtrl" + i.ToString());
                    }
                }
                if (DTEnableColumns.Rows[0]["Taxinclusive"].ToString() == "True" || DTEnableColumns.Rows[0]["ItemTaxType"].ToString() == "0")
                {
                    TextBox txtTaxIncGrandTotalCtrl = (TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString());
                }
            }

        }
    }

    protected void ClearTable()
    {
        LstRowCounter = (List<string>)ViewState["LstRowCounter"];

        LstTdServiceCtrl = (List<string>)ViewState["LstTdServiceCtrl"];
        LstDdlService = (List<string>)ViewState["LstDdlService"];

        LstTdDescriptionCtrl = (List<string>)ViewState["LstTdDescriptionCtrl"];
        LstTxtDescriptionCtrl = (List<string>)ViewState["LstTxtDescriptionCtrl"];

        LstTdYearRefCtrl = (List<string>)ViewState["LstTdYearRefCtrl"];
        LstTxtYearRefCtrl = (List<string>)ViewState["LstTxtYearRefCtrl"];

        LstTdUomCtrl = (List<string>)ViewState["LstTdUomCtrl"];
        LstTxtUomCtrl = (List<string>)ViewState["LstTxtUomCtrl"];

        LstTdUnit2Ctrl = (List<string>)ViewState["LstTdUnit2Ctrl"];
        LstTxtUnit2Ctrl = (List<string>)ViewState["LstTxtUnit2Ctrl"];
        LstTdUnit3Ctrl = (List<string>)ViewState["LstTdUnit3Ctrl"];
        LstTxtUnit3Ctrl = (List<string>)ViewState["LstTxtUnit3Ctrl"];
        LstTdUnit4Ctrl = (List<string>)ViewState["LstTdUnit4Ctrl"];
        LstTxtUnit4Ctrl = (List<string>)ViewState["LstTxtUnit4Ctrl"];

        LstTdUnitRateCtrl = (List<string>)ViewState["LstTdUnitRateCtrl"];
        LstTxtUnitRateCtrl = (List<string>)ViewState["LstTxtUnitRateCtrl"];
        LstTdRateCtrl = (List<string>)ViewState["LstTdRateCtrl"];
        LstTxtRateCtrl = (List<string>)ViewState["LstTxtRateCtrl"];

        LstTdGrossAmtCtrl = (List<string>)ViewState["LstTdGrossAmtCtrl"];
        LstTxtGrossAmtCtrl = (List<string>)ViewState["LstTxtGrossAmtCtrl"];

        LstTdDiscountRateCtrl = (List<string>)ViewState["LstTdDiscountRateCtrl"];
        LstTxtDiscountRateCtrl = (List<string>)ViewState["LstTxtDiscountRateCtrl"];

        LstTdDiscountAmtCtrl = (List<string>)ViewState["LstTdDiscountAmtCtrl"];
        LstTxtDiscountAmtCtrl = (List<string>)ViewState["LstTxtDiscountAmtCtrl"];

        LstTdServiceChargeCtrl = (List<string>)ViewState["LstTdServiceChargeCtrl"];
        LstTxtServiceChargeCtrl = (List<string>)ViewState["LstTxtServiceChargeCtrl"];
        LstTdServiceChargeAmtCtrl = (List<string>)ViewState["LstTdServiceChargeAmtCtrl"];
        LstTxtServiceChargeAmtCtrl = (List<string>)ViewState["LstTxtServiceChargeAmtCtrl"];
        LstTdServiceChargeTotalCtrl = (List<string>)ViewState["LstTdServiceChargeTotalCtrl"];
        LstTxtServiceChargeTotalCtrl = (List<string>)ViewState["LstTxtServiceChargeTotalCtrl"];

        LstTdServiceTaxCtrl = (List<string>)ViewState["LstTdServiceTaxCtrl"];
        LstTxtServiceTaxCtrl = (List<string>)ViewState["LstTxtServiceTaxCtrl"];
        LstTdPECessCtrl = (List<string>)ViewState["LstTdPECessCtrl"];
        LstTxtPECessCtrl = (List<string>)ViewState["LstTxtPECessCtrl"];
        LstTdHECessCtrl = (List<string>)ViewState["LstTdHECessCtrl"];
        LstTxtHECessCtrl = (List<string>)ViewState["LstTxtHECessCtrl"];
        LstTdLuxuryCtrl = (List<string>)ViewState["LstTdLuxuryCtrl"];
        LstTxtLuxuryCtrl = (List<string>)ViewState["LstTxtLuxuryCtrl"];
        LstTdVATCtrl = (List<string>)ViewState["LstTdVATCtrl"];
        LstTxtVATCtrl = (List<string>)ViewState["LstTxtVATCtrl"];

        LstTdTaxIncGrandTotalCtrl = (List<string>)ViewState["LstTdTaxIncGrandTotalCtrl"];
        LstTxtTaxIncGrandTotalCtrl = (List<string>)ViewState["LstTxtTaxIncGrandTotalCtrl"];
        if (tblMainInvoice.Rows.Count > 3)
        {
            for (int i = tblMainInvoice.Rows.Count; i > 3; i--)
            {
                if (LstRowCounter != null)
                {
                    LstRowCounter.RemoveAt(i - 4);

                    LstTdServiceCtrl.RemoveAt(i - 4);
                    LstDdlService.RemoveAt(i - 4);

                    LstTdDescriptionCtrl.RemoveAt(i - 4);
                    LstTxtDescriptionCtrl.RemoveAt(i - 4);
                    LstTdYearRefCtrl.RemoveAt(i - 4);
                    LstTxtYearRefCtrl.RemoveAt(i - 4);

                    LstTdUomCtrl.RemoveAt(i - 4);
                    LstTxtUomCtrl.RemoveAt(i - 4);

                    LstTdUnit2Ctrl.RemoveAt(i - 4);
                    LstTxtUnit2Ctrl.RemoveAt(i - 4);
                    LstTdUnit3Ctrl.RemoveAt(i - 4);
                    LstTxtUnit3Ctrl.RemoveAt(i - 4);
                    LstTdUnit4Ctrl.RemoveAt(i - 4);
                    LstTxtUnit4Ctrl.RemoveAt(i - 4);

                    LstTdUnitRateCtrl.RemoveAt(i - 4);
                    LstTxtUnitRateCtrl.RemoveAt(i - 4);
                    LstTdRateCtrl.RemoveAt(i - 4);
                    LstTxtRateCtrl.RemoveAt(i - 4);

                    LstTdGrossAmtCtrl.RemoveAt(i - 4);
                    LstTxtGrossAmtCtrl.RemoveAt(i - 4);

                    LstTdDiscountRateCtrl.RemoveAt(i - 4);
                    LstTxtDiscountRateCtrl.RemoveAt(i - 4);

                    LstTdDiscountAmtCtrl.RemoveAt(i - 4);
                    LstTxtDiscountAmtCtrl.RemoveAt(i - 4);

                    LstTdServiceChargeCtrl.RemoveAt(i - 4);
                    LstTxtServiceChargeCtrl.RemoveAt(i - 4);
                    LstTdServiceChargeAmtCtrl.RemoveAt(i - 4);
                    LstTxtServiceChargeAmtCtrl.RemoveAt(i - 4);
                    LstTdServiceChargeTotalCtrl.RemoveAt(i - 4);
                    LstTxtServiceChargeTotalCtrl.RemoveAt(i - 4);

                    LstTdServiceTaxCtrl.RemoveAt(i - 4);
                    LstTxtServiceTaxCtrl.RemoveAt(i - 4);
                    LstTdPECessCtrl.RemoveAt(i - 4);
                    LstTxtPECessCtrl.RemoveAt(i - 4);
                    LstTdHECessCtrl.RemoveAt(i - 4);
                    LstTxtHECessCtrl.RemoveAt(i - 4);
                    LstTdLuxuryCtrl.RemoveAt(i - 4);
                    LstTxtLuxuryCtrl.RemoveAt(i - 4);
                    LstTdVATCtrl.RemoveAt(i - 4);
                    LstTxtVATCtrl.RemoveAt(i - 4);

                    LstTdTaxIncGrandTotalCtrl.RemoveAt(i - 4);
                    LstTxtTaxIncGrandTotalCtrl.RemoveAt(i - 4);


                }
                tblMainInvoice.Rows.RemoveAt(i - 1);
            }

            ViewState["LstRowCounter"] = LstRowCounter;

            ViewState["LstTdServiceCtrl"] = LstTdServiceCtrl;
            ViewState["LstDdlService"] = LstDdlService;
            ViewState["LstHidTaxableNonTaxable"] = LstHidTaxableNonTaxable;
            ViewState["LsthidServiceIncomeLid"] = LsthidServiceIncomeLid;

            ViewState["LstTdDescriptionCtrl"] = LstTdDescriptionCtrl;
            ViewState["LstTxtDescriptionCtrl"] = LstTxtDescriptionCtrl;

            ViewState["LstTdYearRefCtrl"] = LstTdYearRefCtrl;
            ViewState["LstTxtYearRefCtrl"] = LstTxtYearRefCtrl;

            ViewState["LstTdUomCtrl"] = LstTdUomCtrl;
            ViewState["LstTxtUomCtrl"] = LstTxtUomCtrl;

            ViewState["LstTdUnit2Ctrl"] = LstTdUnit2Ctrl;
            ViewState["LstTxtUnit2Ctrl"] = LstTxtUnit2Ctrl;
            ViewState["LstTdUnit3Ctrl"] = LstTdUnit3Ctrl;
            ViewState["LstTxtUnit3Ctrl"] = LstTxtUnit3Ctrl;
            ViewState["LstTdUnit4Ctrl"] = LstTdUnit4Ctrl;
            ViewState["LstTxtUnit4Ctrl"] = LstTxtUnit4Ctrl;

            ViewState["LstTdUnitRateCtrl"] = LstTdUnitRateCtrl;
            ViewState["LstTxtUnitRateCtrl"] = LstTxtUnitRateCtrl;
            ViewState["LstTdRateCtrl"] = LstTdRateCtrl;
            ViewState["LstTxtRateCtrl"] = LstTxtRateCtrl;

            ViewState["LstTdGrossAmtCtrl"] = LstTdGrossAmtCtrl;
            ViewState["LstTxtGrossAmtCtrl"] = LstTxtGrossAmtCtrl;

            ViewState["LstTdDiscountRateCtrl"] = LstTdDiscountRateCtrl;
            ViewState["LstTxtDiscountRateCtrl"] = LstTxtDiscountRateCtrl;

            ViewState["LstTdDiscountAmtCtrl"] = LstTdDiscountAmtCtrl;
            ViewState["LstTxtDiscountAmtCtrl"] = LstTxtDiscountAmtCtrl;

            ViewState["LstTdServiceChargeCtrl"] = LstTdServiceChargeCtrl;
            ViewState["listhidServiceCharge"] = listhidServiceCharge;
            ViewState["LstTxtServiceChargeCtrl"] = LstTxtServiceChargeCtrl;
            ViewState["LstTdServiceChargeAmtCtrl"] = LstTdServiceChargeAmtCtrl;
            ViewState["LstTxtServiceChargeAmtCtrl"] = LstTxtServiceChargeAmtCtrl;
            ViewState["LstTdServiceChargeTotalCtrl"] = LstTdServiceChargeTotalCtrl;
            ViewState["LstTxtServiceChargeTotalCtrl"] = LstTxtServiceChargeTotalCtrl;

            ViewState["LstTdServiceTaxCtrl"] = LstTdServiceTaxCtrl;
            ViewState["LstTxtServiceTaxCtrl"] = LstTxtServiceTaxCtrl;
            ViewState["LstTdPECessCtrl"] = LstTdPECessCtrl;
            ViewState["LstTxtPECessCtrl"] = LstTxtPECessCtrl;
            ViewState["LstTdHECessCtrl"] = LstTdHECessCtrl;
            ViewState["LstTxtHECessCtrl"] = LstTxtHECessCtrl;
            ViewState["LstTdLuxuryCtrl"] = LstTdLuxuryCtrl;
            ViewState["LstTxtLuxuryCtrl"] = LstTxtLuxuryCtrl;
            ViewState["LstTdVATCtrl"] = LstTdVATCtrl;
            ViewState["LstTxtVATCtrl"] = LstTxtVATCtrl;

            ViewState["LstTdTaxIncGrandTotalCtrl"] = LstTdTaxIncGrandTotalCtrl;
            ViewState["LstTxtTaxIncGrandTotalCtrl"] = LstTxtTaxIncGrandTotalCtrl;
            hidRowCount.Value = "2";
            counter = 2;
            _ResetControls(tblMainInvoice);
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Welcome.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Save_ACC_SaveVoucher();
        lblMsg.Text = "Inserted Successfully !!!";
    }
    protected void Save_ACC_SaveVoucher() // Save_ACC_SaveTransaction
    {
        SqlParameter[] sparam ={
                                    new SqlParameter("@PARENTTYPEID","2"),
                                    new SqlParameter("@BILLFORMATID",ddlFormat.SelectedValue),
                                    new SqlParameter("@MODULETYPE","3")
                                };
        DataTable DTTranTypeId = DU.getdatausingprocedure("USP_ACC_GetTransactionSettingBasedOnFormat", sparam);
        if (hidTranType.Value != string.Empty)
        {
            string strTranId = string.Empty;
            string strTranDate = string.Empty;
            if (txtDate.Text != string.Empty)
            {
                string[] strTemp = txtDate.Text.Split('/');
                strTranDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];
            }
            string strcurrdate = string.Empty;
            string[] temp = System.DateTime.Now.ToString().Split('/');
            strcurrdate = temp[1] + "/" + temp[0] + "/" + temp[2].ToString();
            SqlParameter[] parmavoucher = { 
                                       new SqlParameter("@TransactionTypeID",Convert.ToString(hidTranType.Value)),
                                       new SqlParameter("@TransactionNumber",Convert.ToString(txtBillNo.Text)),
                                       new SqlParameter("@FinYear",Session["FINYEAR"].ToString()),
                                       new SqlParameter("@ReferenceNumber",Convert.ToString(txtRefNo.Text)),
                                       new SqlParameter("@IsCashVoucher","0"),
                                       new SqlParameter("@TransactionDate",strTranDate),
                                       new SqlParameter("@Narration",txtnarration.Text),
                                       new SqlParameter("@CreditDays",txtCreditDays.Text),
                                       //new SqlParameter("@CreatedOn",strcurrdate),
                                       new SqlParameter("@CreatedBy",Session["userid"].ToString()),
                                       new SqlParameter("@IsCancelled","0"),
                                       new SqlParameter("@CostCentreClassID","0")
                                      };
            int Result = DU.executeprocedure("USP_ACC_SaveTransaction", parmavoucher);
            if (Result > 0)
            {
                string strsql = "Select Ident_Current('ACC_VOUCHERS') as TranId";
                DataTable DTTranId = DU.getdata(strsql);
                if (DTTranId.Rows.Count > 0)
                {
                    Save_ACC_SaveVoucherDetails(DTTranId.Rows[0]["TranId"].ToString());
                }
            }
        }
    }
    protected void Save_ACC_SaveVoucherDetails(string TRANID) // Save_ACC_SaveTransactionDetails
    {
        if (TRANID != string.Empty)
        {
            string VouSerial = string.Empty;
            int count = Convert.ToInt32(hidRowCount.Value);
            double grantotal = 0 - Convert.ToDouble(txtGrandTotalMain.Text);
            SqlParameter[] parmavoucherdetails1 = { 
                                       new SqlParameter("@TransactionID",Convert.ToString(TRANID)),
                                       new SqlParameter("@LedgerID",Convert.ToString(ddlCustomer.SelectedValue)),
                                       new SqlParameter("@Amount",grantotal),//(0-Convert.ToInt32(txtGrandTotalMain.Text)).ToString()
                                       new SqlParameter("@IsReconciled","0"),
                                      };
            int result1 = DU.executeprocedure("USP_ACC_SaveTransactionDetails", parmavoucherdetails1);
            if (result1 > 0)
            {
                string strvou = "Select Ident_Current('ACC_VOUDETAILS') as VouSerial";
                DataTable dt = DU.getdata(strvou);
                if (dt.Rows.Count > 0)
                {
                    lblMsg.Text += " voudetals";
                    VouSerial = dt.Rows[0]["VouSerial"].ToString();
                }
            }
            int result2 = 0;

            for (int i = 1; i <= count; i++)
            {
                string serviceid = ((DropDownList)tblMainInvoice.Rows[i].FindControl("ddlService" + i.ToString())).SelectedValue.ToString();
                if (serviceid != "0")
                {
                    string LID = ((HiddenField)tblMainInvoice.Rows[i].FindControl("hidServiceIncomeLid" + i.ToString())).Value.ToString();
                    string TotalAmount = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeTotalCtrl" + i.ToString())).Text.ToString();

                    SqlParameter[] parmavoucherdetails2 = { 
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",LID),
                                       new SqlParameter("@Amount",TotalAmount),
                                       new SqlParameter("@IsReconciled","0"),
                                      };
                    result2 = DU.executeprocedure("USP_ACC_SaveTransactionDetails", parmavoucherdetails2);
                }
            }
            int result3 = 0;
            int result4 = 0;
            int result5 = 0;
            if (hidSaleLevel.Value == "ItemLevel")
            {
                SqlParameter[] parmavoucherdetails3 = {  // Service Tax
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",hidServiceTaxLid.Value),
                                       new SqlParameter("@Amount",txtServiceTaxAmtMain.Text),
                                       new SqlParameter("@IsReconciled","0"),
                                      };
                result3 = DU.executeprocedure("USP_ACC_SaveTransactionDetails", parmavoucherdetails3);
                SqlParameter[] parmavoucherdetails4 = {  // PECESS Tax
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",hidPECessLid.Value),
                                       new SqlParameter("@Amount",txtPECessAmtMain.Text),
                                       new SqlParameter("@IsReconciled","0"),
                                      };
                result4 = DU.executeprocedure("USP_ACC_SaveTransactionDetails", parmavoucherdetails4);
                SqlParameter[] parmavoucherdetails5 = { // HECESS Tax
                                       new SqlParameter("@TransactionID",TRANID),
                                       new SqlParameter("@LedgerID",hidHECessLid.Value),
                                       new SqlParameter("@Amount",txtHECessAmtMain.Text),
                                       new SqlParameter("@IsReconciled","0"),
                                      };
                result5 = DU.executeprocedure("USP_ACC_SaveTransactionDetails", parmavoucherdetails5);
            }
            if (result1 > 0 && result2 > 0 || result3 > 0 || result4 > 0 || result5 > 0)
            {
                if (VouSerial != string.Empty)
                {
                    Save_ACC_REFDETAILS(VouSerial);
                    Save_IVC_INVOICE_MAIN(TRANID);
                }
            }
        }
    }

    protected void Save_ACC_REFDETAILS(string VouSerial)
    {
        if (VouSerial != string.Empty)
        {
            string strRefDate = string.Empty;
            if (txtDate.Text != string.Empty)
            {
                string[] strTemp = txtDate.Text.Split('/');
                strRefDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];
            }

            string Reference = string.Empty;
            string strsql = "SELECT FORMATNAME FROM IVC_TRANSACTIONFORMAT WHERE FORMATID='" + ddlFormat.SelectedValue + "'";
            DataTable DTFormatName = DU.getdata(strsql);
            if (DTFormatName.Rows.Count > 0)
            {
                Reference += DTFormatName.Rows[0]["FORMATNAME"].ToString() + " ";
                if (txtPrefix.Text != string.Empty)
                {
                    Reference += Convert.ToString(txtPrefix.Text) + "/";
                }
                Reference += txtBillNo.Text;
                if (txtSuffix.Text != string.Empty)
                {
                    Reference += "/" + txtSuffix.Text;
                }
            }
            double grantotal = 0 - Convert.ToDouble(txtGrandTotalMain.Text);

            SqlParameter[] pref = {
                                            new SqlParameter("@ReferenceSerial","0"),
                                            new SqlParameter("@ParentReference","0"),
                                            new SqlParameter("@TransactionSerial",VouSerial),
                                            new SqlParameter("@ReferenceType","1"),
                                            new SqlParameter("@Reference",Reference),
                                            new SqlParameter("@ReferenceDate",strRefDate),
                                            new SqlParameter("@CreditDays",Convert.ToString(txtCreditDays.Text)),
                                            new SqlParameter("@ReferenceAmount",grantotal),//0-Convert.ToInt32(txtGrandTotalMain.Text)
                                            new SqlParameter("@IsTDSDeducted","0")
                                };
            DU.executeprocedure("USP_ACC_SaveReferenceDetails", pref);
        }
    }

    protected void Save_IVC_INVOICE_MAIN(string TRANID)
    {
        if (TRANID != string.Empty)
        {
            SqlParameter[] pmain = { 
                                new SqlParameter("@INVOICEID","0"), // 0 For Insert
                                new SqlParameter("@TRANID",TRANID),
                                new SqlParameter("@FORMATID",ddlFormat.SelectedValue),
                                new SqlParameter("@CUSTOMERLEDGERID",ddlCustomer.SelectedValue),
                                new SqlParameter("@SERVICEORDER",Convert.ToString(txtOtherRef.Text).Trim()),
                                new SqlParameter("@COMMENTS",Convert.ToString(txtremarks.Text)),
                                new SqlParameter("@CHECKEDBY",Convert.ToString(txtcheckedby.Text)),
                                new SqlParameter("@PREPAREDBY",Convert.ToString(txtpreparedby.Text)),
                                new SqlParameter("@SERVICEDBY",Convert.ToString(txtservicedby.Text)),
                                new SqlParameter("@ISTDSAPPLIED","0"),
                                new SqlParameter("@ISROUNDOFFEDITABLE","0"),
                               };
            int result = DU.executeprocedure("USP_IVC_SaveInvoiceMain", pmain);
            if (result > 0)
            {
                string strinvoice = "select Ident_Current('IVC_INVOICE_MAIN')as InvoiceID ";
                DataTable dt = DU.getdata(strinvoice);
                if (dt.Rows.Count > 0)
                {

                    Save_IVC_InvoiceChild(dt.Rows[0]["INVOICEID"].ToString());
                    Save_IVC_InvoiceTotal(dt.Rows[0]["INVOICEID"].ToString());
                }
            }

        }
    }
    protected void Save_IVC_InvoiceChild(string InvoiceID)
    {
        if (InvoiceID != string.Empty)
        {
            int count = Convert.ToInt32(hidRowCount.Value);
            for (int i = 1; i <= count; i++)
            {

                string serviceid = ((DropDownList)tblMainInvoice.Rows[i].FindControl("ddlService" + i.ToString())).SelectedValue.ToString();
                if (serviceid != "0")
                {
                    string adddesc = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDescriptionCtrl" + i.ToString())).Text.ToString();
                    string yearref = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtYearRefCtrl" + i.ToString())).Text.ToString();
                    string uom = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUomCtrl" + i.ToString())).Text.ToString();
                    string unit2 = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit2Ctrl" + i.ToString())).Text.ToString();
                    string unit3 = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit3Ctrl" + i.ToString())).Text.ToString();
                    string unit4 = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnit4Ctrl" + i.ToString())).Text.ToString();
                    string Unit = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtUnitRateCtrl" + i.ToString())).Text.ToString();
                    string Rate = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtRateCtrl" + i.ToString())).Text.ToString();
                    string grossamt = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtGrossAmtCtrl" + i.ToString())).Text.ToString();
                    string discountper = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountRateCtrl" + i.ToString())).Text.ToString();
                    string discountamt = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtDiscountAmtCtrl" + i.ToString())).Text.ToString();
                    string serchvper = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeCtrl" + i.ToString())).Text.ToString();
                    string serchvamt = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeAmtCtrl" + i.ToString())).Text.ToString();
                    string totalamt = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceChargeTotalCtrl" + i.ToString())).Text.ToString();
                    string servicetax_h = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtServiceTaxCtrl" + i.ToString())).Text.ToString();
                    string pecess_h = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtPECessCtrl" + i.ToString())).Text.ToString();
                    string hecess_h = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtHECessCtrl" + i.ToString())).Text.ToString();
                    string luxuary_h = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtLuxuryCtrl" + i.ToString())).Text.ToString();
                    string vat_h = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtVATCtrl" + i.ToString())).Text.ToString();
                    string GrandTotal_h = ((TextBox)tblMainInvoice.Rows[i].FindControl("txtTaxIncGrandTotalCtrl" + i.ToString())).Text.ToString();

                    #region Fill Zero

                    if (unit2 == string.Empty)
                    {
                        unit2 = "0";
                    }
                    if (unit3 == string.Empty)
                    {
                        unit3 = "0";
                    }
                    if (unit4 == string.Empty)
                    {
                        unit4 = "0";
                    }
                    if (Unit == string.Empty)
                    {
                        Unit = "0";
                    }
                    if (Rate == string.Empty)
                    {
                        Rate = "0";
                    }
                    if (grossamt == string.Empty)
                    {
                        grossamt = "0";
                    }
                    if (discountper == string.Empty)
                    {
                        discountper = "0";
                    }
                    if (discountamt == string.Empty)
                    {
                        discountamt = "0";
                    }
                    if (serchvper == string.Empty)
                    {
                        serchvper = "0";
                    }
                    if (serchvamt == string.Empty)
                    {
                        serchvamt = "0";
                    }
                    if (totalamt == string.Empty)
                    {
                        totalamt = "0";
                    }
                    if (servicetax_h == string.Empty)
                    {
                        servicetax_h = "0";
                    }
                    if (pecess_h == string.Empty)
                    {
                        pecess_h = "0";
                    }
                    if (hecess_h == string.Empty)
                    {
                        hecess_h = "0";
                    }
                    if (luxuary_h == string.Empty)
                    {
                        luxuary_h = "0";
                    }
                    if (vat_h == string.Empty)
                    {
                        vat_h = "0";
                    }
                    if (GrandTotal_h == string.Empty)
                    {
                        GrandTotal_h = "0";
                    }
                    #endregion Fill Zero
                    double taxableafterdicount = 0;
                    taxableafterdicount = (Convert.ToDouble(grossamt) - Convert.ToDouble(discountamt));
                    SqlParameter[] pChild = { 
                                new SqlParameter("@INVOICECHILDID","0"), 
                                new SqlParameter("@INVOICEID",InvoiceID),
                                new SqlParameter("@SERVICEID",serviceid),
                                new SqlParameter("@ADDDESC",adddesc),
                                new SqlParameter("@YEARREF",yearref),
                                new SqlParameter("@UNIT",Unit),
                                new SqlParameter("@RATE",Rate),
                                new SqlParameter("@TAXABLEAMT",grossamt),
                                new SqlParameter("@SERVICETAXAMT",servicetax_h),
                                new SqlParameter("@CESS1AMT",pecess_h),
                                new SqlParameter("@CESS2AMT",hecess_h),
                                new SqlParameter("@LTAMT",luxuary_h),
                                new SqlParameter("@DISCOUNT",discountamt),
                                new SqlParameter("@DISCOUNTRATE",discountper),
                                new SqlParameter("@SERVICECHARGERATE",serchvper),
                                new SqlParameter("@SERVICECHARGEAMT",serchvamt),
                                new SqlParameter("@ABTMNTAMT","0"),
                                new SqlParameter("@VATAMT",vat_h),
                                new SqlParameter("@TOTALAMT",GrandTotal_h),
                                new SqlParameter("@UNIT2",unit2),
                                new SqlParameter("@UNIT3",unit3),
                                new SqlParameter("@UNIT4",unit4),
                                new SqlParameter("@UOM",uom),
                                new SqlParameter("@TAXABLEAFTERDIS",taxableafterdicount.ToString()),// (Convert.ToInt32(grossamt)-Convert.ToInt32(discountamt))
                               };
                    int result = DU.executeprocedure("USP_IVC_SaveInvoiceChild", pChild);
                    if (result > 0)
                    {

                    }
                }
            }
        }
    }
    protected void Save_IVC_InvoiceTotal(string InvoiceID)
    {
        if (InvoiceID != string.Empty)
        {
            #region Fill Zero
            if (txtTaxableAmtMain.Text.ToString() == string.Empty)
            {
                txtTaxableAmtMain.Text = "0";
            }
            if (txtNonTaxableAmtMain.Text.ToString() == string.Empty)
            {
                txtNonTaxableAmtMain.Text = "0";
            }
            if (txtTotalAmountMain.Text.ToString() == string.Empty)
            {
                txtTotalAmountMain.Text = "0";
            }
            if (txtServiceTaxRateMain.Text.ToString() == string.Empty)
            {
                txtServiceTaxRateMain.Text = "0";
            }
            if (txtServiceTaxAmtMain.Text.ToString() == string.Empty)
            {
                txtServiceTaxAmtMain.Text = "0";
            }
            if (txtPECessAmtMain.Text.ToString() == string.Empty)
            {
                txtPECessAmtMain.Text = "0";
            }
            if (txtHECessRateMain.Text.ToString() == string.Empty)
            {
                txtHECessRateMain.Text = "0";
            }
            if (txtHECessAmtMain.Text.ToString() == string.Empty)
            {
                txtHECessAmtMain.Text = "0";
            }
            if (txtLuxuaryTaxRateMain.Text.ToString() == string.Empty)
            {
                txtLuxuaryTaxRateMain.Text = "0";
            }
            if (txtLuxuaryTaxMain.Text.ToString() == string.Empty)
            {
                txtLuxuaryTaxMain.Text = "0";
            }
            if (txtRoundOffAmtMain.Text.ToString() == string.Empty)
            {
                txtRoundOffAmtMain.Text = "0";
            }
            if (txtDiscountTaxableAmtMain.Text.ToString() == string.Empty)
            {
                txtDiscountTaxableAmtMain.Text = "0";
            }
            if (txtNonTaxableDiscountAmtMain.Text.ToString() == string.Empty)
            {
                txtNonTaxableDiscountAmtMain.Text = "0";
            }
            if (txtDiscountTaxableRate.Text.ToString() == string.Empty)
            {
                txtDiscountTaxableRate.Text = "0";
            }
            if (txtNonTaxableDiscountRate.Text.ToString() == string.Empty)
            {
                txtNonTaxableDiscountRate.Text = "0";
            }
            if (txtServiceChargeAmtMain.Text.ToString() == string.Empty)
            {
                txtServiceChargeAmtMain.Text = "0";
            }            
            if (txtGrandTotalMain.Text.ToString() == string.Empty)
            {
                txtGrandTotalMain.Text = "0";
            }
            if (txteffectiveratecurr.Text.ToString() == string.Empty)
            {
                txteffectiveratecurr.Text = "0";
            }
            if (txteffectiveratepre.Text.ToString() == string.Empty)
            {
                txteffectiveratepre.Text = "0";
            }
            #endregion Fill Zero
            SqlParameter[] parmaTotal = { 
                                      new SqlParameter("@INVOICEID",InvoiceID),
                                      new SqlParameter("@TAXABLEAMT",txtTaxableAmtMain.Text),
                                      new SqlParameter("@NONTAXABLEAMT",txtNonTaxableAmtMain.Text),
                                      new SqlParameter("@EXPREIMURSEMENTAMT","0"),
                                      new SqlParameter("@TOTALAMTBEFTAX",txtTotalAmountMain.Text),
                                      new SqlParameter("@SERVICETAXRATE",txtServiceTaxRateMain.Text),
                                      new SqlParameter("@SERVICETAXAMT",txtServiceTaxAmtMain.Text),
                                      new SqlParameter("@EDUCESS1RATE",txtPECessRateMain.Text),
                                      new SqlParameter("@EDUCESS1AMT",txtPECessAmtMain.Text),
                                      new SqlParameter("@EDUCESS2RATE",txtHECessRateMain.Text),
                                      new SqlParameter("@EDUCESS2AMT",txtHECessAmtMain.Text),
                                      new SqlParameter("@LUXURYTAXRATE",txtLuxuaryTaxRateMain.Text),
                                      new SqlParameter("@LUXURYTAXAMT",txtLuxuaryTaxMain.Text),
                                      new SqlParameter("@ROUNDOFFAMT",txtRoundOffAmtMain.Text),
                                      new SqlParameter("@EDITEDROUNDOFFAMT","0"),
                                      new SqlParameter("@TAXDISCOUNT",txtDiscountTaxableAmtMain.Text),
                                      new SqlParameter("@NONTAXDISCOUNT",txtNonTaxableDiscountAmtMain.Text),
                                      new SqlParameter("@TAXDISRATE",txtDiscountTaxableRate.Text),
                                      new SqlParameter("@NONTAXDISRATE",txtNonTaxableDiscountRate.Text),
                                      new SqlParameter("@ABTMNTAMT","0"),
                                      new SqlParameter("@SERVICECHARGEAMT",txtServiceChargeAmtMain.Text),
                                      new SqlParameter("@VATAMT",txtVATMain.Text),
                                      new SqlParameter("@TOTALAMT",txtGrandTotalMain.Text),
                                      new SqlParameter("@CURRENTNETTDS","0"),
                                      new SqlParameter("@CURRENTNETAMTPAYABLE","0"),
                                      new SqlParameter("@CURRENTEFFRATE",txteffectiveratecurr.Text),
                                      new SqlParameter("@PREVIOUSNETTDS","0"),
                                      new SqlParameter("@PREVIOUSNETAMTPAYABLE","0"),
                                      new SqlParameter("@PREVIOUSEFFRATE",txteffectiveratepre.Text),
                                      new SqlParameter("@NETTDS","0"),
                                      new SqlParameter("@NETAMTPAYABLE","0"),
                                     };
            int result = DU.executeprocedure("USP_IVC_SaveInvoiceTotal", parmaTotal);
            if (result > 0)
            {
                ddlFormat_SelectedIndex(this, EventArgs.Empty);
                txtOtherRef.Text = string.Empty;
                txtRefNo.Text = string.Empty;
                txtCreditDays.Text = string.Empty;
                txtnarration.Text = string.Empty;
                txtservicedby.Text = string.Empty;
                txtpreparedby.Text = string.Empty;
                txtcheckedby.Text = string.Empty;
                txtremarks.Text = string.Empty;
                lblMsg.Text = " Sales Invoice Created....!!!";

                // Display();
            }
        }
    }
}

//var TaxGrossAmt=parseFloat(parseFloat(parseFloat(parseFloat(servicetaxper)+(parseFloat(pecesstaxper)/10)+(parseFloat(hecesstaxper)/10)*100)/1000)/10);
//alert((parseFloat(parseFloat(servicetaxper)+(parseFloat(pecesstaxper)/10)+(parseFloat(hecesstaxper)/10))*100/1000/10));

//var TaxGrossAmt=(parseFloat(parseFloat(servicetaxper)+(parseFloat(pecesstaxper)/10)+(parseFloat(hecesstaxper)/10))*100/1000/10)+1;

